mrvnk

Is it possible to check for model relation in where clause?

Posted 4 months ago by mrvnk

Hey there, as I am pretty new to Laravel I have a question in mind that drives me crazy, so I decided to ask you. Coming from Symfony I am pretty used to make simple querys with relationship classes and not having to worry about making sure it's the same class. Using Laravel I would just check for the ID which could cause a lot of trouble if somehow my variable contains an object of a different class.

For example: I have Product::class and Marketplace::class and I want to check, if there is a product with a specific name in a specific marketplace. So as for Symfony I would just write:

...->findBy(['marketplace' => $m, 'name' => 'randomName']);

So if somehow $m is an object of class MyTestClass, an exception would be thrown. For Laravel I would do the following, resulting in a valid query

::where('marketplace_id', $marketplace->id)->where('name', 'randomName')->first();

Does anyone understand my concern or am I just too "afraight" of not be informed, if I do a mistake in larger programs or is there a simple / slim way to do the same with Laravel?

Please sign in or create an account to participate in this conversation.