4

I have next SQL query:

SELECT summary_table.device_id, WEEKDAY(summary_table.day) as day, AVG(summary_table.shows) as avg_shows
    FROM (
         SELECT device_id, day, sum(shows) as shows
         FROM statistics
         GROUP BY device_id, day
    ) as summary_table
    WHERE device_id IN (1,2,3) // Just for example
    GROUP BY device_id, WEEKDAY(day)

How should I execute this using Laravel? I put this query in DB::select function, but how can I place all ids in "WHERE device_id IN (?)" condition? I tried using "array(implode(',', $var))" but it doesnt work. If I have ids like "13, 14" I get results only for id = 13.

So the question is how to place instead of "?" array of ids?

1
  • Show code that you tried so far Commented Nov 14, 2013 at 12:16

3 Answers 3

8

Take a look at the docs here, scroll down to "Using Where In With An Array":

http://four.laravel.com/docs/queries

The whereIn method takes an array, so pass in $var directly, no need to implode.

->whereIn('device_id', $var)
Sign up to request clarification or add additional context in comments.

5 Comments

I can write DB::select('..')->whereIn together?
The docs on that page are for the query builder, not a manual sql query
Well I use manual sql query. It seems a little bit difficult to put that query inside query builder?
Because of your subquery? Sure it'll be a bit tricky. You can look at laravel.com/docs/queries#advanced-wheres to see how subqueries work with the querybuilder. Ultimately I think you either use query builder to take advantage of proper binding, or go with @Kasyx's answer and do it manually all the way.
->whereIn('device_id', $var) this will work with. DB::table() not with DB::select().
3

Laravel is using PDO library. Sadly, binding with PDO doesn't work with WHERE IN. You can read about it here.

So what you have to do is to put prepared string in your query like this:

"(...)
    WHERE device_id IN (".array(implode(',', $var)).")
    (...)"

3 Comments

I thought about it. The only trouble is to prevent SQL injection. I've always trusted laravels functions like ->where(). Should I loop through $var and convert in to int like (int)$item before putting it inside implode?
Yes you're right with sql injection danger. You should convert all $var elements into integer.
The best option with converting is intval in my opinion: us1.php.net/intval
0
"(...)
    WHERE device_id IN (".implode(',', $var).")
    (...)"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.