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]
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
Thanks, but I want to find all products from category slug field. What is the correct syntax?
You can use this code
$slug = 'example';
$products = Product::whereHas('category',function($query) use ($slug){
$query->where('slug',$slug)
})->where('is_active','t')->get();
Thank you, that is exactly what I wanted. So I have to use a closure to find records based on a joined table?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community