PHP code example of testmonitor / eloquent-calculated-columns
1. Go to this page and download the library: Download testmonitor/eloquent-calculated-columns library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
testmonitor / eloquent-calculated-columns example snippets
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Model;
use TestMonitor\CalculatedColumns\HasCalculatedColumns;
class User extends Model
{
use HasCalculatedColumns;
public function calculatedColumns(): array
{
return [
'total_price' => function (Builder $query) {
$query->select(DB::raw("SUM(order_items.price) AS total_price"))
->from('order_items')
->whereColumn('order_items.order_id', 'orders.id');
},
];
}
}
use App\Models\Order;
$orders = Order::query()
->withCalculatedColumns()
->get();
}