I have problem with the pagination .
everything work fine without error but the problem is when i use makeHidden with my code it change the structure of my json pagination result
this is my code
$result = Job::where('user_id','=',Auth::id())->paginate(5);
$result= $result->makeHidden(['hasMessage']);
without the second line the result is
{
total: 1 ,
per_page: 5,
current_page: 1,
last_page: 1,
next_page_url: null,
prev_page_url: null,
from: 1,
to: 1,
data: [
{
id: 4,
sid:125,
hasMessage: true
}
]
}
but when i use
$result= $result->makeHidden(['hasMessage']);
I got
[
{
id: 4,
sid:125,
}
]
any idea please ? ? ? is it a bug or there is something wrong ? ?
hasMessage is an append field not a real columns
Use Pagination in Laravel Implementing pagination in laravel is smooth, add the laravel paginate function in the getData() function inside the EmployeeController class. Remove all() and use paginate() and it takes a number as an argument, that number defines the number of results to be shown to the user.
Its simply means that you have an array of values and you want record except that values/records. you can simply pass a array into whereNotIn() laravel function. With query builder $users = DB::table('applications') ->whereNotIn('id', [1,3,5]) ->get(); //will return without applications which contain this id's.
The Eloquent collection object extends Laravel's base collection, so it naturally inherits dozens of methods used to fluently work with the underlying array of Eloquent models. Be sure to review the Laravel collection documentation to learn all about these helpful methods!
finally I did it with small programming trick
$paginator = Job::where('user_id','=',Auth::id())->paginate(5);
$data = $paginator->makeHidden(['hasMessage']);
$paginator->data = $data;
return $paginator;
thank you
if you want to preserve pagination data use getCollection()
and setCollection()
methods:
$paginator = Job::where('user_id','=',Auth::id())->paginate(5);
$paginator->setCollection($paginator->getCollection()->makeHidden(['hasMessage']));
return $paginator;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With