Laravel 5.5 orderby count and paginate - laravel

I'm trying to get to order the results by count in laravel 5.5 but haven't found any simple yet working method so far.
What I have tried for example:
$videos = Video::withCount('com_num')->where('active', '1')->orderByDesc('com_num_count')->paginate(51);
But this just gives me:
Call to undefined method Illuminate\Database\Query\Builder::com_num()
Is this possible somehow while using the Laravel's pagination?

Related

Create pagination from groupBy in laravel 5.8

$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

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));

Laravel eloquent returns different type of result on local and live

On my localhost laravel eloquent returns object while on live server it returns an array.
Why is this happening? is there any setting I need to update?
$parent_user = User::where('email', $organiser->email)->first();
Laravel v5.2

Laravel Scout toSql does not exist

I am trying to get SQL query as a string from query builder. toSql() method works fine without laravel scout. I am using this package for Fulltext search. Here is my code.
Post::toSql(); // Working without scout => select * form post
But I am getting an error when try to search
Post::search('test')->toSql();
Method Laravel\Scout\Builder::toSql does not exist
How Laravel Scout can be extended to add a method toSql to it.
Try this
DB::enableQueryLog();
Post::search('test');
dd(DB::getQueryLog());

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