mercredi 20 janvier 2016

Passing value as array in Where clause in Laravel

I have an array in $qids as [{"qid":1},{"qid":2},{"qid":3},{"qid":4}], Now, I want to get rows from database matching these qid value. I am working on my Laravel project and the where clause I am using is as follows

$questions = Question::where(function($q) use ($qids){
    foreach($qids as $key => $value){
        $q->where($key, '=', $value);
    }
})->get();

This gives me an error

*SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from `questions` where (`0` = {"qid":1} and `1` = {"qid":2} and `2` = {"qid":3} and `3` = {"qid":4}))*

As I can see, in the error line

where (`0` = {"qid":1} and `1` = {"qid":2} and `2` = {"qid":3} and `3` = {"qid":4})

it is taking 0, 1, 2, 3 as key and whole {"qid":1} as value.

FYI, I am generating $qids from the statement.

$qids = Examquestion::select('qid')->where('examid', $examid)->get();

is there any way that I could save only values in $qids rather than pair. Hope you understand the scenario. TIA.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire