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

448
Visualizações
laravel use with() , pluck() together

Query Output

\App\Post :

function PostMeta(){
    return $this->hasMany('App\PostMeta');
}

And my Query : ( pluck is not working) -

I need to use less database queries

$query = \App\Post
      ::with(array('PostMeta'=>function($query){
          $query->pluck('key','value');
      }));
       $query->get();

I need to get title_en , But I cant use pluck here!

New Update

solved:

function get_PostMeta(){
      // print_r($this->relations['PostMeta']);
      return $this->relations['PostMeta'];
    }


$query = \App\Post::with('PostMeta')->get();
      foreach ($query as $key => $post){
        $post->meta = $post->get_PostMeta()->pluck('value', 'key');
      }
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can't do this while eager loading the data, but you can use collection method with the same name pluck() in the view when accessing the relation data:

{{ $post->postMeta->pluck('value', 'key') }}

In this case, you'll avoid N+1 problem and get data in the format you want.

Update

Since you want to prepare data for Vue, here's a small example of how you could iterate over the collection:

foreach ($posts as $post) {
    $post->meta = $post->postMeta->pluck('value', 'key');
    unset($post->postMeta);
}
over 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