Reputation: 2069
I have a huge query that sometimes will require me to use paginate() and other times will require me to use get()
. Is there a way to create some kind of if statement right at the end of the query that will determine which one to use? Kinda like a where subquery.
Other solutions I came up with was to set the paginate number to a really high number to mimic get()
, but I'd much rather do it the other way if it's possible to use some kind of if statement but I wasn't really sure how to figure it out.
Thanks
Upvotes: 1
Views: 791
Reputation: 6393
you CAN put conditionals.
e.g. Let's say your database code is like this:
$data = DB::table('test')->get(); // or paginate
then
$temp = DB::table('test');
if(condition)
{
return $temp->paginate();
} else {
return $temp->get();
}
if you want to check what type of data, it is returning (in controller)
if($data instanceof \Illuminate\Pagination\LengthAwarePaginator)
{
//it is a paginator object. write the code accordingly
} else {
// It is returning array.
}
Though you should further validate to see what's coming through.
you can call the items()
method on the paginator object to get the results with reference to that particluar page.
e.g. if $data
is a paginator object, then,
dd($data->items());
will print the items as an array. (items()
returns the resultset as an array)
Upvotes: 2