Support the ongoing development of Laravel.io →
Database Eloquent

Hello, I am getting Unknown column 'categories.slug' What am I doing wrong in L5?

Thanks

[code] $products = Product::with('category') ->where('products.is_active', '=', 't') ->where('categories.slug', '=', $slug) ->get(); [/code]

[code] Product Class public function category() { return $this->hasOne('App\Category', 'category_id', 'id'); } [/code]

[code] Category Class public function product() { return $this->belongsTo('App\Product', 'id', 'category_id'); } [/code]

Last updated 2 years ago.
0

You wrong in this code

$products = Product::with('category') ->where('products.is_active', '=', 't') ->where('categories.slug', '=', $slug) ->get();

This code only list products with category's id.

Error

Unknown column 'categories.slug'

It's mean haven't column 'categories.slug' in product's table.

I think you don't need check slug because all products has been check by categories.id

Last updated 9 years ago.
0

Thanks, but I want to find all products from category slug field. What is the correct syntax?

0

You can use this code

$slug = 'example';
$products = Product::whereHas('category',function($query) use ($slug){
	$query->where('slug',$slug)
})->where('is_active','t')->get();
0

Thank you, that is exactly what I wanted. So I have to use a closure to find records based on a joined table?

0

Yeah. that right

0

Sign in to participate in this thread!

Eventy

Your banner here too?

frocco frocco Joined 3 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.