ich mit Laravel und Eloquent ORM arbeitete und nach der Entwicklung Controller, Modell und Ansicht für meinen Tisch brauche ich Agregate Informationen zu extrahieren, aber ich weicht nicht, herauszufinden, ist der beste Weg, oder die sauberste " Laravel "Weg, das zu tun.Laravel Gruppe mit Elequent Modell
Ich habe eine db Tabelle wie in diesem Beispiel:
Schema::create('order_items', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('order_id')->unsigned();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->string('item_description');
$table->integer('item_qty');
$table->status('item_status');
});
Die Detail Daten können wie:
2 2017-02-28 12:48:07 2017-02-28 12:48:31 1 ProductB 2 NEW
4 2017-02-28 12:48:17 2017-02-28 12:48:17 1 ProductC 3 NEW
29 2017-03-10 10:49:47 2017-03-10 10:49:47 1 ProductC 23 CLOSED
40 2017-03-10 10:49:47 2017-03-10 10:49:47 1 ProductB 2 SHIPPED
1 2017-02-28 11:04:28 2017-02-28 11:29:10 3 ProductA 1 NEW
28 2017-03-10 10:49:47 2017-03-10 10:49:47 3 ProductB 22 CLOSED
39 2017-03-10 10:49:47 2017-03-10 10:49:47 3 ProductA 1 SHIPPED
5 2017-02-28 14:36:54 2017-02-28 14:36:54 6 ProductD 4 NEW
6 2017-02-28 14:37:01 2017-02-28 14:37:01 6 ProductD 5 NEW
30 2017-03-10 10:49:47 2017-03-10 10:49:47 6 ProductD 24 CLOSED
41 2017-03-10 10:49:47 2017-03-10 10:49:47 6 ProductC 3 SHIPPED
Im Controller ich einen Bereich
verwendenpublic function home()
{
$onlynew = Orderitem::onlynew();
return view('home', compact('onlynew '));
}
Das Modell ist
public function scopeonlynew ($query) {
return \DB::select('SELECT item_description, sum(item_qty) qty
FROM order_items
WHERE item_status = ?
GROUP BY item_description',['NEW']);
}
Nach Ansicht I-Daten auf diese Weise zugreifen können
<div class="row">
<ul>
@foreach ($onlynew as $newitem)
<li>{{$newitem->item_description}} - {{$newitem->qty}}</li>
@endforeach
</ul>
</div>
Ist es möglich, eine Syntax wie die folgende Beispiel oder für diese Art der Abfrage zu verwenden das Rahmenwerk erlaubt nicht, den Erbauer zu benutzen?
return $query->where('item_status', '=', 'NEW')
->sum('item_qty')
->groupBy('item_description')
->orderBy('item_description');
Vielen Dank für jede Art von Hilfe oder Anregung.
** selectRaw ** war genau das, was ich suchte und die Dokumentation, die ich vermisse. Vielen Dank. – AlexMI