badr's avatar
Level 1

the correct way to use makeHidden in laravel

Hi , 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 ? ?

0 likes
5 replies
Snapey's avatar

either hide the field in your model or use select to get just the fields you need

badr's avatar
Level 1

@Snapey

'hasMessage' is an append failed not a columns . because of that i can't select it and i will get an error .

and i can't hide it inside my model because it's visible in another place

badr's avatar
Level 1

@Snapey I did it :D

this is what i did

$result = Job::where('user_id','=',Auth::id())->paginate(5);
 $data =$result;
 $result= $result->makeHidden(['hasMessage']);
 $data->data = $result;
return $data;

thank you anyway .

mahdihty's avatar

Hi @badr the simplest way is

tap(Job::where('user_id','=',Auth::id())->paginate(5))->makeHidden(['hasMessage'])

use tap function

1 like

Please or to participate in this conversation.