eloquent - Laravel: Use scope from related model to filter results -


tltr:

how collection of 1 model (users) using scope method (usersubscriber)? models in one-to-one relation.

more about:

i have 2 model related one-to-one:

second model users has extended, specific role (subscribers in case)

user model:

class user extends \sentinel\models\user {     public function subscriber()     {                 return $this->hasone('app\models\usersubscriber', 'user_id');     }        } 

userextended model:

class usersubscriber extends model {       public function user()     {                 return $this->belongsto('app\models\user', 'user_id');     }              public function scopelangsubscribers($query, $langcode)     {         $query->where('languages_subscribed', 'like', '%' . $langcode . '%');     }     } 

the goal is:

get collection of users using scope usersubscriber.

(without rewriting scope method user model.)

where stuck:

            $users = user::with(['subscriber' => function ($query) use ($lang) {                 $query->langsubscribers($lang);             }])->get(); 

this returns me collection of users 'subscriber' set null of them.

i've tried filter them, but:

  • it not best solution, because iterates on entire big collection of users after retrieving of them db

            $users = $users->filter(function($item) {             return $item->subscriber != null;         });  

'subscriber' not column, can't do:

->wherenotnull('subscriber') 

how can accomplish this?

edit:

alternatively, maybe there way convert 1 collection (of subscribers) (users) afterretrieving first, can done aesy , clean:

$userssubscribers = usersubscriber::langsubscribers($lang)->get(); 

use wherehas instead.

$users = user::with('subscriber')->wherehas('subscriber', function    ($query) use ($lang) {     $query->langsubscribers($lang); })->get(); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -