Metodos Raw

Para añadir consultas tipo Raw  en Eloquent, contamos con algunos métodos muy útiles. Aquí muestro  ejemplos con tres  metodos Raw ( whereRaw, havinRaw y orderByRaw)

 // whereRaw
$orders = DB::table('orders')
    ->whereRaw('price > IF(state = "TX", ?, 100)', [200])
    ->get();

// havingRaw
Product::groupBy('category_id')->havingRaw('COUNT(*) > 1')->get();

// orderByRaw
User::where('created_at', '>', '2016-01-01')
  ->orderByRaw('(updated_at - created_at) desc')
  ->get();

Y listo, esto es todo.

Más sobre Laravel en mi blog

Espero modestamente que este artículo, sirva de ayuda a alguien.

Gracias.

Subir montañas hermana hombres……

Translate »