为什么DB :: select有效,但DB :: table不起作用?

时间:2019-08-03 00:32:19

标签: laravel-5 eloquent

我有一个查询要从下表中选择数据:

sed

在刀片视图中,我正在使用以下代码来显示数据:

public function getOptions($id)
    {
        $questionOptions = DB::table('question_options')
            ->where('question_id', '1')
            ->orderBy('order_number');
        //$questionOptions = DB::select('select * from question_options where question_id = '.$id.' order by order_number desc');

        //$questionOptions = QuestionOption::all();
        $question = Question::find($id);

        return view('question.option', ['question' => $question, 'questionOptions' => $questionOptions]);
    }

当我使用DB :: select时,它工作正常。 使用DB :: table无效。错误如下。

@foreach($questionOptions as $item)
    <tr>
        <td>{{$item->id}}</td>
        <td>{{$item->option}}</td>
        <td>{{$item->image}}</td>
        <td>{{$item->order_number}}</td>
        <td>{{$item->is_correct}}</td>
    </tr>
@endforeach

请帮助我,如何与DB :: table一起使用?

非常感谢你, BienHV

1 个答案:

答案 0 :(得分:0)

只需添加->get()即可:

$questionOptions = DB::table('question_options')
            ->where('question_id', '1')
            ->orderBy('order_number')->get();