Method paginate does not exist in laravel collection - laravel

hi I want to paginate rows by relation by writing some codes like this
$jobs=auth()->user()->employer->jobs::paginate(10);
I tried this code but I got this error
Method Illuminate\Database\Eloquent\Collection::paginate does not
exist.

The last part should probably be ->jobs()->paginate(10);.

If you write relation without parentheses "()" it will return collection , while with "()" will return models that paginate methods available on it.
So your code should :
$jobs=auth()->user()->employer->jobs()::paginate(10);

Try this:
$jobs=auth()->user()->employer->jobs->paginate(10);

Related

Laravel Paginate not working with specific model or table

Paginate not working for specific model. with all Models, paginate is working fine except one specific model. Also tried with query builder.
$doubts = DB::table('users')->paginate(10);
dd($doubts->count());
//output: 10
$doubts = DB::table('doubts')->paginate();
dd($doubts->count());
//output: 0
$doubts = DB::table('doubts')->get();
dd($doubts->count());
//output: 8
Don't know what am I missing here. Please help.
Maybe I think you have to put
How many results you want to show per page
So if you want 8
Then I think it should be something like this
$doubts = DB::table('doubts')->paginate(8);
dd($doubts->count());
And if you are using pages Number under your data then you might need the query builder.
You should pass number argument inside paginate function.
Look at laravel pagination documentation
And make sure that DB::table('doubts')
Is returning collection
I thing you must add parameter in paginate(), to show how much data that show per page. You can read documentation in : https://laravel.com/docs/9.x/pagination#paginating-query-builder-results
https://laravel.com/docs/9.x/pagination#cursor-pagination
because you are not add parameter in paginate, it means not data that show every page.
$doubts = DB::table('doubts')->paginate(10);

How to use orderBy with Laravel 6 LazyCollection?

It's been a few days since the Laravel released its latest version 6. I am trying to implement Laravel LazyCollection instead of the normal Collection class.
Following line works-
Drug::where('deactive',0)->orderBy('code')->get();
And when I use
Drug::cursor()->where('deactive',0)->orderBy('code')->get();
I get an error Method Illuminate\Support\LazyCollection::orderBy does not exist.
Can anyone help me how to use OrderBy with LazyCollection?
UPDATE
After getting the answer here is the correct syntax of above query
Drug::cursor()->where('deactive',0)->sortBy('code');
Offcourse, orderBy method is Query builder method. You can use sortBy as defined in the docs
I think you can do this.
Drug::where('deactive',0)->orderBy('code')->cursor();

put single row to variable and delete it at same time with one query

I have a password_resets table in mysql database and i want to get single raw of data and delete it with one query in Lumen with DB facade like this:
$reset_row = DB::table('password_resets')->where('token', $request->token)->first()->delete();
But i have a error :
Call to undefined method stdClass::delete()
i try this code :
$reset_row = DB::table('password_resets')->where('token', $request->token)
//do my work whith $reset_row->first();
$reset_row->delete();
But i think this way use 2 query to do this work.
NOTE : i know i can not delete and reason is first() method )return it to array)
Is there any way to do this?
Actually no need to use ->first(). Bcz your token is unique.
if you need try this.
$reset_row = DB::table('password_resets')->where('token', $request->token)
->Limit(1)->delete();
I think it is possible to use "limit" with "delete".
You can simply chain the delete method to your Query Buider.
No need to select the first on since you want to delete them, not select them.
DB::table('password_resets')->where('token', $request->token)->delete();
Its already answered in another post in StackOverflow.
You can use this answer too
laravel-delete-query-builder and you can also look at the official documentation about deleting using the query builder
At the end I Think to create model for reset password and Use it like this:
1. set primary key to token by set it on model like this
2. use find method to find token (document for find method)
use this way:
$reset=PasswordReset::find($token);
//work with $reset
$reset->delete()
NOTE: : for set primary key to string col see this link too

Is there a way to tell if this object is an eloquent object or not?

I'm trying to generalize turning arrays into json and it's getting a little messed up when I added eloquent models into the mix.
Is there a way to tell if this object is an eloquent object or not?
Like an isEloquent() function or something.
or you can use this
$isEloquent = ($yourObject instanceof Illuminate\Database\Eloquent\Model);
You can use the PHP function is_subclass_of
$isEloquent = is_subclass_of($yourObject, 'Illuminate\Database\Eloquent\Model');

Laravel 4 pagination

My pagination doesn't seem to work. Here's how I done it:
$players = Auth::user()->players->paginate(2);
And then done all the stuff that's needed in the view, but:
Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()
It shows the line, where that variable is.
Try
Auth::user()->players()->paginate(2);
Auth::user()->players returns a collection, the paginate function belongs to the Builder class of Eloquent.
Nevermind, solved it myself:
Solution:
Auth::user()->players()->paginate(2);

Resources