0

I use Laravel DB::select to return specific data from DB,

the problem is only one row return when use print_r($result)

    $questions = DB::select('
            select text
            from question 
            where id in (?) 
            order by field(id,?)',
            array($question_ids_list_str,$question_ids_list_str));


    print_r($questions);

how to return the rest of results? thanks,

5
  • There is no rest of results apparently. This method returns array of stdObjects representing each row. Btw why do you use this instead of query builder, that's easier to use? Commented Aug 31, 2014 at 10:06
  • You should make sure what $question_ids_list_str contains. If it contains only one id or one existing id in database, select won't return more rows Commented Aug 31, 2014 at 10:08
  • @MarcinNabiałek $question_ids_list_str contains list of ids , tested Commented Aug 31, 2014 at 10:15
  • @JarekTkaczyk_deczo_ I use this way cause I work on DB with too many tables :( Commented Aug 31, 2014 at 10:16
  • @mwafi I have no idea what you mean by that. I wasn't talking about Eloquent, but query builder: DB::table('question')->whereIn('id', $ids)->orderBy(DB::raw('field(id,'.implode(',',$ids).')'))->get(); Commented Aug 31, 2014 at 11:49

2 Answers 2

2

You may try something like this:

$ids = array(1, 2, 3); // Pass the ids in whereIn clause
$questions = DB::table('question')
               ->whereIn('id', $ids) 
               ->orderBy('id')
               ->get(array('text')); // Only get text field
Sign up to request clarification or add additional context in comments.

1 Comment

this is working fine, but what is the wrong in DB::select, force it to retrieve only 1 row?
0

Try this:

$questions = DB::select('
        select text
        from question 
        where id in (?) 
        order by field(id,?)',
        array($question_ids_list_str,$question_ids_list_str));
foreach($questions as $question) {
    echo $question;
}

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.