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

443
Views
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');
      }
about 4 years ago · Santiago Trujillo
1 answers
Answer question

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);
}
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!