SELECT apartment_id, COUNT(created_at) AS n_view,
DATE_FORMAT(created_at, '%Y-%m-%d') as day_view
FROM views
WHERE apartment_id=36 GROUP BY day_view
ORDER BY COUNT(created_at) DESC

Wie kann ich diese MySQL-Abfrage in einen Laravel-Abfrage-Generator übersetzen?

1
folipso 19 Feb. 2020 im 14:55

4 Antworten

Beste Antwort

Überprüfen Sie die Laravel-Dokumente unter Abfrage-Generator und sehen Sie sich die Beispiele an. Sie können Ihr SQL problemlos in den Abfrage-Generator konvertieren ::

use Illuminate\Support\Facades\DB;

DB::table('views')
    ->selectRaw("apartment_id, COUNT(created_at) AS n_view, DATE_FORMAT(created_at, '%Y-%m-%d') as day_view")
    ->where('apartment_id', '=', 36)
    ->groupBy('day_view')
    ->orderByRaw('COUNT(created_at) DESC')
    ->get();
0
Hafez Divandari 19 Feb. 2020 im 12:41
DB::table('views')
    ->selectRaw("apartment_id, COUNT(created_at) AS n_view, DATE_FORMAT(created_at, '%Y-%m-%d') as day_view")
    ->where("apartment_id", "=", "36")
    ->groupBy('day_view')
    ->orderBy('n_view', "DESC")
    ->get();
1
Hashmat Ullah 19 Feb. 2020 im 12:14

Sie können dies mit App \ Views tun. benutze DB;

Views::select('apartment_id', DB::raw("COUNT(created_at) as n_view") ,DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d') as day_view"))->where('apartment_id' ,36)->orderBy('n_view', 'desc')->get();
0
Lalit Kumar 19 Feb. 2020 im 12:13
$data = $this->views;
$data = $data->select(
    "apartment_id",
    DB::raw("COUNT(created_at) AS n_view"),
    DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d') as day_view")
);
$data = $data->where("apartment_id", 36);
$data = $data->groupBy("day_view");
$data = $data->orderBy("created_at", "desc");
$data = $data->get();
0
Nishabh Mistry 19 Feb. 2020 im 12:39