Create pagination from groupBy in laravel 5.8 - laravel

$payments = Payment::with('borrowerUser','collectorUser')
->get()
->groupBy('date_paid');
which gives me this result
My question is, is it possible for me to put a pagination in this scenario?
I already tried
$payments = Payment::listings()
->with('borrowerUser','collectorUser')
->groupBy('date_paid')->paginate(5);
and put {{$payments->listings()}} on my blade
but that doesn't work.
any suggestions? thank you in advance!

put this code in controller:
$payments = Payment::with('borrowerUser','collectorUser')
->groupBy('date_paid')->paginate(5);
put below code in blade file after for loop is completed
{!! $payments ->render() !!}

This is the must read content from the laravel documentation
Currently, pagination operations that use a groupBy statement cannot
be executed efficiently by Laravel. If you need to use a groupBy with
a paginated result set, it is recommended that you query the database
and create a paginator manually.
Documentation link
You can use orderBy clause after groupBy to get the result, but still it won't give you efficient results

Related

Trying to create a GROUP BY with eloquent

$collections = Collection::selectRaw("DATE(created_at) as collection_date, sum(gross_amount) as amount")
->groupBy('collection_date')
->paginate(10);
When I do that it displays that collection_date does not exist in the table, so I wonder how can I create a group by with just DATE() in eloquent?
Thanks
Based on Laravel docs, pagination doesn't work with the groupBy clause properly.
https://laravel.com/docs/7.x/pagination#paginating-query-builder-results
Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.

How to paginate the query results when using Laravel eloquent relationships

I have the following query which retrieves the child configuration records (confch) associated with the parent configuration (confp) and it works fine.
$data["items"] = Confp::find(decrypt($type))->Confch;
I just need to paginate the results.
adding ->paginate(10) would definitly not work as it does not exist in the resulting collection. I found nothing helping in the laravel documentation.
How can I do that please?
Try this instead of above code.
$confp = Confp::find(decrypt($type));
$data["items"] = $confp->setRelation('Confch',$confp->Confch()->paginate(10));

How can I retrieve this in laravel?

Flowing query is working fine on mysql database but how can I retrieve this in laravel? code of mysql is given bellow:
select count('brand_id') from products where brand_id=3
You could do
$number = 3;
DB::table('products')->where('brand_id',$number)->count();
But I recommend using Models
Product::where('brand_id',$number)->count();
Following what laravel has on https://laravel.com/docs/5.6/queries at the aggregates topic:
$products = DB::table('products')->where('brand_id', 3)->count();
Welcome to Stack Overflow!
As #PlayMa256 said, you can do exactly this query using
DB::table('products')->where('brand_id', 3)->count();
BUT, if you have defined your models correctly, you can do this directly using you model class. If your model is called Product, then:
Product::where('brand_id', 3)->count();
Another awesome way to do this is by using the magic methods for where clause:
Product::whereBrandId(3)->count();

How to check eloquent queries in laravel 5?

Im trying to check the eloquent queries to check for n+1 problems.
Im using this "Event::listen('illuminate.query', function($sql){
var_dump($sql);
});" on top of web.php routes file but no query appears. It seems that is because of the version of laravel, that is laravel 5.
Do you know in this version how to properly check the eloquent queries?
Add this to AppServiceProvider::boot() (documentation):
DB::listen(function ($query) {
var_dump($query);
});
try this library dude https://github.com/barryvdh/laravel-debugbar , you will know all of your query

laravel eloquent lists - Sorting A List Of Column Values

Using laravel and I'm creating a select box on a form. I've been using the helper to create the select box and all is working fine.
I retrieve the data for the select box from a database and use the following to retrieve the data:
$data = model::lists('name','id')
Again all works ok and this returns the expected array
My problem though is I can't seem to sort this list - i've tried adding orderBy() but no joy.
Other than using a native php function is there a laravel method to sort a list?
You can put whatever you want, and then list it. I mean:
model::orderBy('orderByColumn')->lists('name', 'id');
As long as lists is the last method in the chain, other methods works just fine.
Starting from Laravel version 5.3 lists is going to be deprecated, use pluck instead:
model::orderBy('orderByColumn')->pluck('name', 'id');
You can try:
$data = model::select('name','id')->orderBy('name');
If that doesn't work, toss a ->get() on the end:
$data = model::select('name','id')->orderBy('name')->get();
when I
dd(YourModel::pluck('name', 'id'));
I see it is Collection Class so I found collection laravel and then I found method sortKeys()
so I do:
YourModel::pluck('name', 'id')->sortKeys();

Resources