97

I want to use Eloquent's active record building to build a search query, but it is going to be a LIKE search. I have found the User::find($term) or User::find(1), but this is not generating a like statement. I'm not looking for a direct answer, but if someone could at least give me a direction to look in that'd be great!

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Jonathan
  • 1,135
  • 1
  • 10
  • 15
  • 2
    http://laravel.com/docs/database/eloquent .. you can use documentation ıt is very clear. – ytsejam Nov 14 '12 at 20:33
  • 2
    I've seen this page I just didn't see anything about searching with wildcards. I also didn't want to set up a regex in a foreach loop as there are hundreds of thousands of rows – Jonathan Nov 14 '12 at 20:36
  • $email = DB::table('users')->where('id', '=', 1)->only('email'); – ytsejam Nov 14 '12 at 20:38
  • it is called fluent query builder in the docs. – ytsejam Nov 14 '12 at 20:50
  • If I could mark the answer and your comment as the answer, I would. Thank you for getting me in the right direction – Jonathan Nov 14 '12 at 21:11
  • You're looking for a full-text seach, it's necesary use fluent. You can use this information http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html – Fernando Montoya Nov 26 '12 at 15:01

5 Answers5

244

You're able to do database finds using LIKE with this syntax:

Model::where('column', 'LIKE', '%value%')->get();
Joel Larson
  • 3,064
  • 1
  • 15
  • 12
  • 1
    not so efficient, throws "Fatal error: Maximum function nesting level of '100' reached, aborting! in... " – Sasi varna kumar Sep 26 '15 at 21:20
  • @gsk I think you join (using the with() method) and then you can search the column as usual. The syntax is probably something like `table.field`. – Anthony Mar 02 '16 at 21:04
69

If you need to frequently use LIKE, you can simplify the problem a bit. A custom method like () can be created in the model that inherits the Eloquent ORM:

public  function scopeLike($query, $field, $value){
        return $query->where($field, 'LIKE', "%$value%");
}

So then you can use this method in such way:

User::like('name', 'Tomas')->get();
Yaroslav
  • 3,168
  • 1
  • 15
  • 14
  • This is the more 'Laravel' way of doing this. It's just cleaner and allows you to adjust the scope in one place, instead of having to go around and adjust each `->where()`. – Daniel Dewhurst Oct 06 '16 at 15:06
  • really love this answer. This make the model and its usage very elegant which is what laravel is about. – deathemperor Mar 06 '18 at 09:19
29

FYI, the list of operators (containing like and all others) is in code:

/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php

protected $operators = array(
    '=', '<', '>', '<=', '>=', '<>', '!=',
    'like', 'not like', 'between', 'ilike',
    '&', '|', '^', '<<', '>>',
    'rlike', 'regexp', 'not regexp',
);

disclaimer:

Joel Larson's answer is correct. Got my upvote.

I'm hoping this answer sheds more light on what's available via the Eloquent ORM (points people in the right direct). Whilst a link to documentation would be far better, that link has proven itself elusive.

dean grande
  • 734
  • 13
  • 15
18

Use double quotes instead of single quote eg :

where('customer.name', 'LIKE', "%$findcustomer%")

Below is my code:

public function searchCustomer($findcustomer)
{
    $customer = DB::table('customer')
                  ->where('customer.name', 'LIKE', "%$findcustomer%")
                  ->orWhere('customer.phone', 'LIKE', "%$findcustomer%")
                  ->get();

    return View::make("your view here");
}
msturdy
  • 10,479
  • 11
  • 41
  • 52
Myint Thu Lwin
  • 229
  • 1
  • 3
  • 8
3

If you do not like double quotes like me, this will work for you with single quotes:

$value = Input::get('q');
$books = Book::where('name', 'LIKE', '%' . $value . '%')->limit(25)->get();

return view('pages/search/index', compact('books'));
Sinan Eldem
  • 5,564
  • 3
  • 36
  • 37