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

jok3r's avatar
Level 1

How optimize that query?

create a form where I must, make a dynamic , and for that I execute this query that returns the data, but I think I could optimize this query with eloquent, some idea:

Brand::join("customers", "customers.fk_uid", "brands.fk_uid") ->where("brands.fk_uid", $request->id) ->pluck('brands.brand','brands.id')->all();

0 likes
3 replies
vajid's avatar

assuming you have setup relation properly

Brand::with('customers')->where('fk_uid',$request->id)->get()->pluck(['brand','id']);
Cronix's avatar
Cronix
Best Answer
Level 67

What is the point of getting customers at all in that query? It looks like all you're doing is getting the brand and id from the brands table by the brands.fk_uid, so why do you need to go through customers at all?

Brand::where('fk_uid', $request->id)->pluck('brand', 'id');

?

1 like
jok3r's avatar
Level 1

jajaja !! exactly, my fault. i was distracted. Thak you ! @Cronix

Please or to participate in this conversation.