vendredi 2 août 2019

Why DB::select working but DB::table doesn't work?

I have a query to select data from the table as below:

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]);
    }

In the blade view, I am using this code to display the data:

@foreach($questionOptions as $item)
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
@endforeach

When I using DB::select it worked fine. Using the DB::table it didn't work. The error as below.

ErrorException (E_ERROR)
Undefined property: Illuminate\Database\MySqlConnection::$id

Please help me, how to using with DB::table?

Thank you so much, BienHV



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire