Mingeun Kim
Mingeun Kim

Reputation: 19

laravel5 Eloquent and Query builder

I'm trying to achieve something like the following:

"SELECT * FROM lists WHERE school_name LIKE '%" . $search . "%'"

Eloquent and Query builder

Thanks in advance.

Upvotes: 1

Views: 69

Answers (1)

jhnbrn
jhnbrn

Reputation: 231

$results = List::where('school_name', 'like', '%' . $search . '%')->get();

This assumes that you have a model called 'List' and a database table called 'lists'. If you do not have a model directly coupled, you could use the following query:

$results = DB::table('lists')->where('school_name', 'like', '%' . $search . '%')->get();

You can also only get the first result by using first() instead of get()

Here is the documentation describing the query builder: https://laravel.com/docs/5.6/queries

Upvotes: 1

Related Questions