Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PabloT's avatar

Laravel 5.5 query (sub-query) pluck.

Hi, Please, I have:

Laravel v5.5.48

Tables:

  • ads
  • ad_settings
  • special_sections

table: ads (among others):

  • id (9)
  • title

table: ad_settings (among others):

  • id (45)
  • ad_id (9)
  • is_active

table: special_sections (among others):

  • id (1)
  • ad_setting_id (45)
  • theme

In my controller I have:

$ad = AdSetting::with(["ad" => function($q){
    $q->where('ad_id', '=', 'id');
}])->pluck('ad_id', 'id' );

The above gives me:

Collection {#14946 ▼
  #items: array:14 [▼
    9 => 45
  ]
}

dd($ad) gives me the "ad id" (9) and the "ad_setting id" (45)

I'm struggling to pluck the ad_setting id (45) with the ad title. Do I need another (inner) query?

I'm really confused now, can someone please give some advice. Thank you.

0 likes
2 replies

Please or to participate in this conversation.