Skip to content
Advertisement

Laravel Eloquent: select records where one field equals another

I apologize if this is duplicated. Is it possible in Eloquent ORM to select records where one field equals another? In MySQL this means:

SELECT Id, Url, ModelId 
WHERE Url LIKE CONCAT('%%', ModelId, '%%')


Is it possible to do so in Eloquent or I’m bound to using raw queries?

Advertisement

Answer

On field equalling another in the query builder (and eloquent) is whereColumn('Url', 'LIKE', 'ModelId') but since you have additional things in the second part you will need to use raw queries like e.g.

DB::table('table')
   ->select('Id', 'Url', 'ModelId')
   ->where('Url', 'LIKE', DB::raw("CONCAT('%%', ModelId, '%%')"));

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement