Sunday, May 21, 2017

Laravel: Is there a smarter way to return only users records instead of filtering them in each query

In my Laravel application I have the following models:
User, Business, Profile, Subscriptions.
A user will have one business, a business will have one profile and many subscriptions.

In my Subscription controller many of my methods used to pull down the logged in users subscriptions require me to add something like the following:

Subscription::where('business_id', '=', Auth::user()->business->id)->active()->get();

In one query this may not be a problem, but I have many methods which pull the businesses subscription records and would like to know is there a smarter way to get only the businesses records while also adding additional scopes to the query.



via Imran

Advertisement