Paco Pinazo Guna
Paco Pinazo Guna

Reputation: 117

LRAVEL Use of undefined constant questions - assumed 'questions' in controller

I am doing this query in a controller:


public static function listarPreguntasHoy($pagina=1, $cantidad=10){


    if($cantidad<=0){
        $cantidad=10;
            }

            if($pagina<1){
                $pagina=1;
            }
        $pagina--;
$saltar = $pagina*10;


$preguntas = Question::select('questions.id', 'questions.id_user', DATE_FORMAT(questions.date, '%Y-%m-%d'), 'questions.title', 'questions.description', 'users.id', 'users.first_name', 'users.last_name')
->join('users', 'users.id', 'questions.id_user')
->where(DATE(questions.date) == CURDATE())
->get()
->skip($saltar)
->take($cantidad)
->toJson();

echo "<pre>";
 var_dump($preguntas);
echo "</pre>";
die;
return view('preguntasRespuestas')->with('preguntas', json_decode($preguntas));

}

And it throws me this error.

ErrorException Use of undefined constant questions - assumed 'questions' (this will throw an Error in a future version of PHP).

Any ideas? Thanks!

Upvotes: 0

Views: 327

Answers (1)

aynber
aynber

Reputation: 23000

You need to quote those two sections, but more than that, since you're using built-in MySQL functions, you need to tell the query to use raw MySQL:

$preguntas = Question::select('questions.id', 'questions.id_user', DB::raw("DATE_FORMAT(questions.date, '%Y-%m-%d')"), 'questions.title', 'questions.description', 'users.id', 'users.first_name', 'users.last_name')
->join('users', 'users.id', 'questions.id_user')
->whereRaw("DATE(questions.date) = CURDATE()")
->get()
->skip($saltar)
->take($cantidad)
->toJson();

Upvotes: 2

Related Questions