How to split the result of Laravel paginator - laravel

I am using the Laravel paginator on my site. I want to split the query result into 4 parts. Because my design has 2 rows of 3 items each and another 2 rows of 2 items each. So I can't use a single foreach loop for showing the items. In my controller, I have the below code.
$properties = Property::whereHas('createdBy', function ($query) {
$query->where('enabled','=',true);
})->orderBy('created_at', 'desc')->paginate(10);
And in my view, I am trying to split the result. I have tried $properties->take(3) and $properties->chunk(3). But is not working.
Below is the content of paginator.
Please help.

Related

I want to extract the first five data that match the where clause from Laravel relationships

I'd like to do something like this with Laravel's Eloquent:relationship, but it doesn't work.
$playlist->setRelation('tags', $playlist->tags->where('privacySetting', 'public')->take(5));
It works without where clause, but I want to retrieve the first 5 data in the tags relationship table that match the where clause.
How can I do this?
Laravel version is 7.28.1.
this will select top 5 based on criteria from model
$playlist = Playlist::with(['tags' => function($filter){
return $filter->where('privacySetting', 'public')
->take(5);
}])
->get();
//try dd($playlist);

Return laravel query results in chunks

I have an Update model in my Laravel/Vue.js app, instead of retrieving and displaying all results at once in my component, I want to return them in chunks of fives and place a NEXT FIVE UPDATES link in my component to display the next 5 update records, much like in pagination. I tried this:
$update = Update::limit(5)->get();
This does not achieve my desired result. Is there a Laravel method i can use in my laravel backend to send the results in chunks of 5 to my Vue.Js component and then make my NEXT FIVE UPDATES link display the next 5 records.
If you're able to send some kind of page or offset value to the backend then you could use laravel's skip and take methods: https://laravel.com/docs/5.7/queries#ordering-grouping-limit-and-offset
$skip = $request->page; //assuming this variable exists...
$limit = 5;
$update = Update::skip($skip * $limit)->take($limit)->get();

orderBy-querybuilder and pagination in laravel 5

I'm trying to make an orderBy in my query and a pagination for the items from a gallery, but instead of sort the complete query, laravel order only page-by-page content, so if I order desc it only works 1 page, when I go to the next page I can find values really different (ordered, but without relationship with the first page).
Here is my query:
$product = DB::table("product")
->leftJoin("provider","product.provider_id","=","provider.id")
->orderBy("daily_sales","DESC")
->select('product.*','provider.comercial_name')
->paginate(6);
You can try:
$product = DB::table("product")
->leftJoin("provider","product.provider_id","=","provider.id")
->select('product.*','provider.comercial_name')
->orderBy("daily_sales","DESC")
->paginate(6);

Using count in an eloquent query

$data = User::where(function($q){
$q->where(User::whereIn('access', array(1,2,3,4))->count(), 4);
})->get();
I only want users who have an access of 1,2,3,4 (used this for illustrative purposes but this is usually a passed in array).
I am getting all users where the count of a where in query equals 4.
How can this be done in eloquent? My above code errors, as it is trying to select all from users where 4 = 4.
Where am I going wrong?

Show page:2 directly in View page in CAKEPHP

Is there any option in $this->Paginator->Settings() to paginate a table and store second or third page to the variable using
$view=$this->Paginate('Post');
You can specify the page with the following:
$this->Paginate->settings(array('page' => 2));
$posts = $this->Paginate->paginate('Post');
$posts now contains the records for page 2

Resources