Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
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

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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

about 4 years ago · Santiago Trujillo Report

0

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;
about 4 years ago · Santiago Trujillo Report

0

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!