在laravel中使用makeHidden的正确方法

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

You are missing toArray() in your code. It should be like:

$result= $result->makeHidden(['hasMessage'])->toArray();

Have a look at the docs:

https://laravel.com/docs/5.4/eloquent-serialization#hiding-attributes-from-json

Edit:

I also have tried to paginate and it did return the changed array and it is the expected output for the makeHidden().

You could also have a look at the function:

public function makeHidden($attributes)
    {
        $attributes = (array) $attributes;

        $this->visible = array_diff($this->visible, $attributes);

        $this->hidden = array_unique(array_merge($this->hidden, $attributes));

        return $this;
    }

As it is doing array_merge it distotes your json response.

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