0

How can I go about producing a query that will allow me to search my table to find any entries where the data field ends with the given value, like the following SQL would allow:

SELECT * FROM table
WHERE 'value' LIKE CONCAT('%', data)

Currently I can do the normal 'LIKE' syntax as follows:

return $query->where('data', 'LIKE', "%$value");

But it doesn't seem to support the reverse, where I am looking for the value in the data column ending with the $value input.

While I could use a raw query, I wanted to know if this was possible to do within Laravel's query builders so that I can ensure proper escaping, etc.

(Sorry for previous lack of info, pressed enter before I was finished and it posted question immediately)

Blam
  • 2,838
  • 3
  • 23
  • 39
  • 2
    Have you [looked at the docs](https://laravel.com/docs/5.5/queries#where-clauses)? What have you tried? What are your models? – Don't Panic Oct 02 '17 at 17:18
  • Pressed enter by accident while still making post - I have updated to add more information – Blam Oct 02 '17 at 17:20
  • `%$value` will do what you describe, right? `$value` at the end of the field. – Don't Panic Oct 02 '17 at 17:26
  • I think you're stuck with a `DB::raw` here. – ceejayoz Oct 02 '17 at 17:26
  • @iCoders that matches `$value` anywhere in the field, which is not what OP asked for. – Don't Panic Oct 02 '17 at 17:27
  • I want the reverse - trying to find where the $value ends with the data in the DB. Example `$value` = `Hello World` would match `World` in DB. – Blam Oct 02 '17 at 17:27
  • I think right here you will have to do it after query. Fetch all the results with the normal `LIKE` and then do the magic pos-query. – null Oct 02 '17 at 21:06

0 Answers0