Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0
$user_details = User::where(''user_name','=',$userName')->skip(0)->take(6)->get();

Skip tells how many of the rows to skip, take shows how many of the rows to take

SQL reference

Sorry it took so long to reply

Last updated 2 years ago.
0

Oh, and apparently you can probably skip the ->skip(0) unless you need it

Last updated 2 years ago.
0

Sounds like the has() method would work better for this situation.

http://laravel.com/docs/eloquent#querying-relations

Last updated 2 years ago.
0
$collectionList = Collection::with(array('items' => function($query)
        {
        	$user_details = User::where('user_name','=',$userName)->first();
            $query->where('user_id','=',$user_details->id);
        }))->get();

I'm probably wrong...

Last updated 2 years ago.
0

cryode said:

Sounds like the has() method would work better for this situation.

http://laravel.com/docs/eloquent#querying-relations

    $posts = Collection::whereHas('items', function($q)
        {
            $q->where('user_id', '=', $user_details->id);

        })->get();

Same issue

  1. I cant use $user_details->id , i declared the variable but just cant use it . I dont know why.
  2. If there is a Fix for 1 , how do i limit the items? Get all Collection with 6 Items each(limit 6)
Last updated 2 years ago.
0

Solved

Problem 1 Solution public function itemsLimitSix() { return $this->BelongstoMany('Item')->limit(6); }

Problem 2 Solution http://laravel.io/forum/02-08-2014-using-variable-in-db-function-solved

On top of that , i also tested with DB::getQueryLog(); , It's working fine with the Limit in it.

Credit to @rufhausen

Last updated 2 years ago.
0

I was hoping to see a solution in your thread, but unfortunately I didn't. As you, I wanted to limit the eager loaded results per result. Setting the limit like this public function itemsLimitSix() { return $this->BelongstoMany('Item')->limit(6); } it is exactly the same as applying the limit in the constraint:

$posts = Collection::whereHas('items', function($q)
{
    $q->where('user_id', '=', $user_details->id)->limit(6);
})->get();

This will limit the count of ALL items return to 6, and not to 6 per post as you want. The same problem is asked here: http://stackoverflow.com/questions/16960079/limit-eager-loaded-comments

I would be glad to hear a solution.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.