1

I would like to query my visitors records from my visitors table that has the the OS substring of bot.

enter image description here

As you can see, there are 3 of 8 records listed here right now. I just want to grab those 3.

I tried look in this documentation. I didn't really find what I am looking to do there, unless I missed it.

$visitors = Visitor::orderBy('created_at', 'desc')
        ->where('os',substr('bot'))<----- NEED help here 
        ->get();

Update

I believe my question is not a duplicate of this proposed duplicate.

halfer
  • 19,824
  • 17
  • 99
  • 186
code-8
  • 54,650
  • 106
  • 352
  • 604
  • 1
    This question has nothing to do with laravel, this is a pure sql one. The duplicate topic explains you how to filter on a substring. – Shadow Mar 03 '17 at 04:59
  • Laravel where() usally take in 3 arguemnts like this : `->where('os', '____', '%bot%')` The second arguments is not as flexible as pure SQL as you think. – code-8 Mar 03 '17 at 05:03
  • When people look for the answer of this question, they should see this `->where('os', 'like', '%bot%')` as answer ... which is axactly what @Eddy answered. – code-8 Mar 03 '17 at 05:05
  • And your claim that this question has not been asked in laravel context before is equally untrue: http://stackoverflow.com/questions/38631486/laravel-query-model-if-values-contain-a-certain-string-taken-from-search-inpu – Shadow Mar 03 '17 at 05:10
  • 2
    No, people need to **understand** how they can query a database and from there they can translate it to any ORM. If you knew how to do this query in sql, then you would not have had to ask this question. – Shadow Mar 03 '17 at 05:14

1 Answers1

3

Did you try with 'LIKE'

$visitors = Visitor::orderBy('created_at', 'desc')
->where('os', 'like', '%bot%') //<----- Match any word containing ...bot.. at any place
->get();

Using a variable

$visitors = Visitor::orderBy('created_at', 'desc')
->where('os', 'like', '%'. $bot .'%') //<----- Match the searched variable
->get();
EddyTheDove
  • 12,979
  • 2
  • 37
  • 45