Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda