Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ferba24's avatar

Can't see the complete query so I don't know what's wrong

I have the following function: public function second($degree_id) {

    $question = DB::table('questions')
        ->select('questions.question_text','questions.answer_time_minutes','questions.answer_time_seconds')
        ->join('universities', 'universities.university_id', 'questions.university_id')
        ->orderByRaw('RAND()')
        ->where('questions.degree_id', $degree_id)
        ->take(1)->first();

    return view('2ndscreen', compact('question'));
}

This query it is supposed to:

  • Get some columns from question table (question_text, questions.answer_time_minutes and questions.answer_time_seconds)
  • Match university_id columns from questions and universities tables
  • Random order
  • Show the questions whose questions.degree_id is equal to the value passed through the function.

Is there any way to see this query in raw format so I can find out what's wrong?

Thank you

0 likes
10 replies
ferba24's avatar

->toSql (); returns Call to a member function toSql() on null

ferba24's avatar

I just inserted it at the end:

    $question = DB::table('questions')
        ->select('questions.question_text','questions.answer_time_minutes','questions.answer_time_seconds')
        ->join('universities', 'universities.university_id', 'questions.university_id')
        ->orderByRaw('RAND()')
        ->where('questions.degree_id', $degree_id)
        ->take(1)->first()
        ->toSql ();
ferba24's avatar

the query logs returns an empty array:

[]

    $question = DB::table('questions')
        ->select('questions.question_text','questions.answer_time_minutes','questions.answer_time_seconds')
        ->join('universities', 'universities.university_id', 'questions.university_id')
        ->orderByRaw('RAND()')
        ->where('questions.degree_id', $degree_id)
        ->take(1)->first();

    DB::enableQueryLog();
    dd(DB::getQueryLog());
ab_dev's avatar
ab_dev
Best Answer
Level 2
DB::enableQueryLog();
 $question = DB::table('questions')
        ->select('questions.question_text','questions.answer_time_minutes','questions.answer_time_seconds')
        ->join('universities', 'universities.university_id', 'questions.university_id')
        ->orderByRaw('RAND()')
        ->where('questions.degree_id', $degree_id)
        ->take(1)->first();
dd(DB::getQueryLog());

Also try,

$question = DB::table('questions')
        ->select('questions.question_text','questions.answer_time_minutes','questions.answer_time_seconds')
        ->join('universities', 'universities.university_id', 'questions.university_id')
        ->orderByRaw('RAND()')
        ->where('questions.degree_id', $degree_id)
        ->take(1)->toSql();
ferba24's avatar

The 2nd returns Trying to get property 'question_text' of non-object

The 1st one returns the query :)

Cronix's avatar

Just FYI

->toSql ();

should replace get()/first() etc. in the query. You can't execute the query (which get()/first() does) and then get the query using that method. Use it instead of executing the query just to get the sql that it compiles.

Please or to participate in this conversation.