Reputation: 11
I am very new to Laravel and here I have one simple question. I need to join one more table to the following
return $products = Product::where('product_type_id', 1)
->where('quantity', '>=', 50)
->where('deleted', 0)
->orderBy('price')
->paginate(100);
I have another table name product_img where all the images of products are stored. Need to fetch all the records where product id is available in product_img table and where file is not equal to 'Noimg.jpg'. I tried different solutions but didn't worked. How to join two tables in Laravel?
I tried to do the following:
$products = Product::leftJoin('product_img', function($join) {
$join->on('products.id', '=', 'product_img.product_id')
->where('product_img.file', '!=', 'Noimg.jpg');
}) ;
->where('quantity', '>=', 100)
->where('deleted', 0)
->orderBy('price')
->paginate(100);
Upvotes: 0
Views: 68
Reputation: 26
The correct symbol for Not Equal is <> in SQL, and != is for programming languages.
$products = Product::join('product_img', function($join) {
$join->on('products.id', '=', 'product_img.product_id')
->where('product_img.file', '<>', 'Noimg.jpg');
}) ;
->where('quantity', '>=', 100)
->where('deleted', 0)
->orderBy('price')
->paginate(100);
Upvotes: 0
Reputation: 331
$products = Product::join('product_img', 'products.id', '=', 'product_img.product_id')
->where('product_img.file', '!=', 'Noimg.jpg')
->where('quantity', '>=', 100)
->where('deleted', 0)
->orderBy('price')
->paginate(100);
Upvotes: 1