0

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);
1
  • replace leftJoin with join method. Commented Dec 11, 2019 at 11:37

2 Answers 2

1
$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);
Sign up to request clarification or add additional context in comments.

Comments

0

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);

3 Comments

query worked but products are listing where records with Noimg.jpg or where records are not in the image table
I need to run this query in Laravel -> SELECT a.* FROM products as a JOIN product_img b ON a.id = b.product_id WHERE b.product_id = a.id AND b.file!='Noimg.jpg' GROUP BY id DESC ORDER BY created_at DESC
yes, It didn't worked.. Actually what I am getting in the raw query Is not what I am getting when I run the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.