Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the correct way to use makeHidden in laravel

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

like image 391
programmer Avatar asked Apr 16 '17 11:04

programmer


People also ask

How can I use pagination in laravel?

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.

What is whereNotIn in laravel?

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.

What is eloquent collection in laravel?

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!


2 Answers

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

like image 181
programmer Avatar answered Oct 06 '22 07:10

programmer


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;
like image 39
m.nikzad Avatar answered Oct 06 '22 06:10

m.nikzad