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

frezno's avatar
Level 36

Accessing individual fields from joined tables

let's say i've three tables:

table products
id
sku
stockstatus_id
manufacturer_id

table stockstatus
id
name

table manufacturers
id
name

the query

$products = DB::table('products')
            ->leftJoin('stockstatus', 'stockstatus_id', '=', 'stockstatus.id')
            ->leftJoin('manufacturers', 'manufacturer_id', '=', 'manufacturers.id')
            ->get();

// and passing it to the view blade:
return view('front.productslist', compact('products');

within blade, how can it get values of stockstatus name and units name? I'm feeling quite dump not to getting this handled...

0 likes
5 replies
frezno's avatar
Level 36

hmmm, even if using an alias for the joined tables, like

$products = DB::table('products')
            ->leftJoin('stockstatus as s', 'stockstatus_id', '=', 's.id')
            ->leftJoin('manufacturers as m', 'manufacturer_id', '=', 'm.id')
            ->get();

i don't know how to get the individual fields displayed via blade.
no problem if the indivudual field names are different but with the same fieldnames but different tables i'm stuck.

// no individual 'name'
@foreach ($products as $p)
    SKU: {{ $p->sku }} - Stock: {{ $p->name }} - Manufacturer: {{ $p->name }} <br>
@endforeach
// doesn't work (error):
@foreach ($products as $p)
    SKU: {{ $p->sku }} - Stock: {{ $p->s.name }} - Manufacturer: {{ $p->m.name }} <br>
@endforeach

Any hints?

bobbybouwmann's avatar

I think you have to do something like this

$products = DB::table('products')
    ->addSelect(DB::raw('stockstatus.name as stockstatusName'))
    ->leftJoin('stockstatus as s', 'stockstatus_id', '=', 's.id')
    ->leftJoin('manufacturers as m', 'manufacturer_id', '=', 'm.id')
    ->get();

Now you can access it like this

@foreach($products as $product)
    {{ $product->stockstatusName }}
@endforeach
isaackearl's avatar

try doing a var_dump to $products and see what is in there. That should help determine how to access it (you could paste that here on laracasts as well).

phildawson's avatar

Curly braces for accessing . attributes

$obj = new \StdClass;
$obj->{'foo.bar'} = 'baz';

echo $obj->{'foo.bar'};
frezno's avatar
Level 36

you pointed me to the right direction, @bobbybouwmann

as convenient as using a framework might be, you little by little forget the programming basics.

Actually i always used unique fieldnames before and that way never ran into the situation as mentioned above.
When using joins the joined fieldnames have to be aliased if they are non unique.
That's what i usually would have done (famous last words ;) ) since i usually don't use select * but call the needed fields.

Long story short, here's the solution, in case someone else runs into the same problem:
(don't like the DB::raw as long as it can be avoided)

$products = DB::table('products')
            ->select('sku', 'stockstatus.name as status', 'manufacturers.name as manu')
            ->leftJoin('stockstatus', 'stockstatus_id', '=', 'stockstatus.id')
            ->leftJoin('manufacturers', 'manufacturer_id', '=', 'manufacturers.id')
            ->get();

and in the blade file:

@foreach ($products as $p)
    SKU: {{ $p->sku }} - Stock: {{ $p->status }} - Manufacturer: {{ $p->manu }} <br>
@endforeach

Please or to participate in this conversation.