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.

5
  • 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. Commented Mar 3, 2017 at 4: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. Commented Mar 3, 2017 at 5: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. Commented Mar 3, 2017 at 5:05
  • And your claim that this question has not been asked in laravel context before is equally untrue: stackoverflow.com/questions/38631486/… Commented Mar 3, 2017 at 5: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. Commented Mar 3, 2017 at 5:14

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

4 Comments

Ohh really ? I didn't know how LIKE work ... Oh ok. I will give it a shot.
what is the oposite of LIKE ? If I want to check the negative case ..
Is there such thing is NOT LIKE ?
Yes. 'not like' would work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.