0

Can any Laravel developers help me understand why the following SQL statement is not working. I don't get any errors, it simply doesn't do what it should:

 DB::table('devices')
->leftJoin('hotel_device','hotel_device.device_id','devices.id')
->where("hotel_device.device_id","IS","NULL")
->delete();

This should remove all device ids where the leftJoin link returns as NULL (ie. where this device id is not being used in the "hotel_device" table).

If I run it as raw SQL directly in the database it works correctly. Note the "IS" condition rather than "=" condition, as I am refering to a NULL value. (using "=" didn't find any matching rows)

thanks.

1
  • I am positive eloquent uses ->whereNull('col') instead of ->where('col', 'IS', 'NULL'), could that be the issue? Commented Jan 9, 2020 at 15:03

1 Answer 1

1

You should use the whereNull method to compare to null rather than trying to filter it using the regular where method.

DB::table('devices')
->leftJoin('hotel_device','hotel_device.device_id','devices.id')
->whereNull("hotel_device.device_id")
->delete();
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, many thanks. I hadn't realised whereNull was a thing! Works perfectly.

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.