AtyFlow
AtyFlow

Reputation: 35

Why can't I use the alias? using raw LARAVEL

The problem is, that the alias fechas doesn't take any value... I can't use it

>    $tabla_c2 = DB::table('horarios')
>     ->select('id_voluntario',DB::raw("DATE_FORMAT(fecha,'%y-%m-%d')as fechas"))
>     ->where('id_voluntario','=', $temp)
>     ->where('fechas','=', $datee)
>     ->get();

Upvotes: 0

Views: 2475

Answers (1)

James
James

Reputation: 16359

As discussed in the comments, this is a SQL limitation - you cannot use an alias for a column in a where clause.

One workaround, as suggested by @ljubadr, is to use having instead - although I will side with @Jeffrey on this and say it is going to unnecessarily make your query slower.

Rather than use the alias, you can utilise whereRaw():

$tabla_c2 = DB::table('horarios')
 ->select('id_voluntario',DB::raw("DATE_FORMAT(fecha,'%y-%m-%d')as fechas"))
 ->where('id_voluntario','=', $temp)
 ->whereRaw("DATE_FORMAT(fecha,'%y-%m-%d') = ?", $datee)
 ->get();

See the docs under Eloquent Aggregates.

Upvotes: 1

Related Questions