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

jawi289p's avatar

Joining Two Tables using Query Builder in Laravel

Hello I want to join two tables in laravel using query builder. I want to display the sku of particular product on the product page. I have tried but only first row is showing. Please can anyone help me in this issue. Below are the tables

products(id, name, price)

products_attributes(id, product_id(FK), sku)

products(1, toy1, 10) (2, toy2, 12)

products_attributes( 1, 1, 7866)(2, 2, 7767)

0 likes
14 replies
Sinnbeck's avatar

Show your current query that isn't working

jawi289p's avatar

@Sinnbeck $result['productattr']=DB::table('products_attr') ->select('products_attr.', 'products.') ->join('products','products.id','=','products_attr.product_id') ->get();

jawi289p's avatar

@Sinnbeck I have used this but I am getting only the first row of the products_attributes

using this in blade view {{$productattr[0]->sku}}

How can I show the sku of the particular product?

Sinnbeck's avatar

@jawi289p yes? You are asking for just index 0?

@foreach ($productattr as $prod) 
{{$prod->sku}}
@endforeach 
Sinnbeck's avatar

@jawi289p sorry still confused. Show what you are getting vs what you are expecting. You can just write it in a code block

Or show the surrounding code in that view

Nakov's avatar

So in your Product model you should have a relationship like this:

public function productAttributes()
{
	return $this->hasOne(ProductAttribute::class);
}

then it is easy:

$products = Product::with('productAttributes')->get();

foreach($products as $product)
{
 // $product->productAttributes->sku;
}

That's in case there is only one product attributes row. Make sure if your table is named products_attributes in your ProductAttribute class add protected $table = 'products_attributes';

Nakov's avatar

@jawi289p then add .* so it selects the columns:

DB::table('products_attr')
	->select('products_attr.*', 'products.*')
	->join('products','products.id','=','products_attr.product_id')
	->get();

but that will return a collection of all product attributes.. if you want a specific one you'll need to add a where clause and use the product id

jawi289p's avatar

@Nakov I have used this but I am getting only the first row of the products_attributes

using this in blade view {{$productattr[0]->sku}}

How can I show the sku of the particular product?

Nakov's avatar
Nakov
Best Answer
Level 73

The reason why you are getting the first row is because of this [0] you see.. that will always return the first row no matter how many you have. I showed you a good way above, and I don't see a reason why not to use the models but the query builder instead.. if you want to use the query builder you still can, just reverse the joins:

$products = DB::table('products')
	->select('products.id as product_id, products.name, products.price', 'products_attr.sku')
	->join('products_attr','products.id','=','products_attr.product_id')
	->get();

$products->firstWhere('product_id', 1)->sku ?? null;

Please or to participate in this conversation.