user8555937
user8555937

Reputation: 2387

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?

Upvotes: 0

Views: 1540

Answers (1)

apokryfos
apokryfos

Reputation: 40653

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, '%')"));

Upvotes: 1

Related Questions