laravel - Applying more query conditions after calling function -


if have like:

$users = helpers::getusersfollowing($user_id); 

where getusersfollowing() looks this:

public static function getusersfollowing($user_id) {     return follow::where('user_id', $user_id)         ->select('username')         ->get(); } 

is possible to, without modifying code in function, apply more queries after function call, like:

$users = helpers::getusersfollowing($user_id)     ->with('posts')     ->select('display_name')     ->get(); 

i need able select display names (in addition usernames) , user's posts well, again, without modifying code in function.

how can this?

yes, should able this:

as jensaronsson pointed out in comments, better solution because doesn't result in n+1 query

$users = helpers::getusersfollowing($user_id);  $user->load('posts')  // if need additional query constraints $users->load(['author' => function ($query) {     $query->orderby('whatever', 'asc'); }]); 

see laravel docs: querying relationships

for work follow-model needs have relationship set this:

public function posts() {     return $this->hasmany('app\post'); // replace 'app\post' model! } 

the old solution (not recommended)

foreach ($users $user) {     $user->posts()->get() } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -