Custom Column Filament

#Model: OrderItem.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderItem extends Model
{
use HasFactory;

protected $fillable = [
'order_id', 'package_id', 'weight', 'unit_price'
];
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderItem extends Model
{
use HasFactory;

protected $fillable = [
'order_id', 'package_id', 'weight', 'unit_price'
];
}
#Model: Order.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Order extends Model
{
use HasFactory;

protected $fillable = [
'customer_id', 'code', 'status', 'payment', 'notes'
];

public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}

public function package(): BelongsTo
{
return $this->belongsTo(Package::class);
}

public function items(): HasMany
{
return $this->hasMany(OrderItem::class);
}
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Order extends Model
{
use HasFactory;

protected $fillable = [
'customer_id', 'code', 'status', 'payment', 'notes'
];

public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}

public function package(): BelongsTo
{
return $this->belongsTo(Package::class);
}

public function items(): HasMany
{
return $this->hasMany(OrderItem::class);
}
}
#File: OrderResource.php
use Filament\Tables\Table;
use App\Models\OrderItem;

public function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('total_price')
->getStateUsing(fn (OrderItem $record) => $record->weight . $record->unit_price),
]);
}
use Filament\Tables\Table;
use App\Models\OrderItem;

public function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('total_price')
->getStateUsing(fn (OrderItem $record) => $record->weight . $record->unit_price),
]);
}
How to display total price from order_item table? so weight * unit_price
12 Replies
toeknee
toeknee12mo ago
->getStateUsing(fn (OrderItem $record) => (int) $record->weight * (int) $record->unit_price /100 ),
->getStateUsing(fn (OrderItem $record) => (int) $record->weight * (int) $record->unit_price /100 ),
The above presuming you are storing cents? You can also adjust the eloquent query to return a sum at the MySQL level
MuhamDaily 🅥
MuhamDaily 🅥OP12mo ago
I got an error message:
App\Filament\Resources\OrderResource::App\Filament\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\OrderItem, App\Models\Order given, called in D:\filament\laundry-corner\vendor\filament\support\src\Concerns\EvaluatesClosures.php on line 35
App\Filament\Resources\OrderResource::App\Filament\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\OrderItem, App\Models\Order given, called in D:\filament\laundry-corner\vendor\filament\support\src\Concerns\EvaluatesClosures.php on line 35
MuhamDaily 🅥
MuhamDaily 🅥OP12mo ago
orders table
No description
MuhamDaily 🅥
MuhamDaily 🅥OP12mo ago
order_items table
No description
Jyrki
Jyrki12mo ago
It's because you are in the order resource. So you'll have to change OrderItem to Order and then loop through all order items to calculate the sum
MuhamDaily 🅥
MuhamDaily 🅥OP12mo ago
Is there no other way? so that the OrderResource table can retrieve an OrderItem?
toeknee
toeknee12mo ago
Above is kinda right, you want to go through the items so instead of $record, as you have a relationship of items you want to look through them items. Why not just have a sum relationship on the model in that case and just return that instance which handles it for it
Jyrki
Jyrki12mo ago
But you have a hasmany relationship. So you cannot fetch just 1 item for the order
MuhamDaily 🅥
MuhamDaily 🅥OP12mo ago
I'm just learning to use filament, so I don't really understand it
Jyrki
Jyrki12mo ago
It's not really filament related, more Laravel. You can also try the sum method on your column https://filamentphp.com/docs/3.x/tables/columns/relationships#aggregating-relationships
MuhamDaily 🅥
MuhamDaily 🅥OP12mo ago
It's been resolved, thanks to the help you gave. This is the code I fixed
TextColumn::make('total_price')
->getStateUsing(function (Order $record) {
// Assuming you have a 'hasMany' relationship named 'items'
$orderItems = $record->items;

// Calculate total_price by summing up the total_price attributeach OrderItem
$totalPrice = $orderItems->sum('total_price');

return $totalPrice;
}),
TextColumn::make('total_price')
->getStateUsing(function (Order $record) {
// Assuming you have a 'hasMany' relationship named 'items'
$orderItems = $record->items;

// Calculate total_price by summing up the total_price attributeach OrderItem
$totalPrice = $orderItems->sum('total_price');

return $totalPrice;
}),
toeknee
toeknee12mo ago
Greate
Want results from more Discord servers?
Add your server