How can i display products in order table & add product filter in order page?

We have 3 tables here order-> orderLineItem -> product How can i connect relation between order & product ? One order can have multiple products
10 Replies
CORONEL
CORONEL5mo ago
You gonna have to describe much better what you trying to accomplish, and what you already got. We can't even tell if its related do Filamentphp or not...
Hiteksha
HitekshaOP5mo ago
I want to display products in my order table in filamentphp but order is not directly connected with product. Order is connected with orderLineItem & orderLineItem have product_id and i want to display name so i need to again connect with product so how can i display that in order table ? I want to put product filter in order page so how can i add that? I want to display products in my order table in filamentphp but order is not directly connected with product. Order is connected with orderLineItem & orderLineItem have product_id and i want to display name so i need to again connect with product so how can i display that in order table ? I want to put product filter in order page so how can i add that?
Dennis Koch
Dennis Koch5mo ago
How are they connected? How would you fetch them with Laravel only?
gladjanus43
gladjanus435mo ago
If I understand correct maybe this is what you are searching for? In App\Models\Order
public function products(): HasManyThrough
{
return $this->hasManyThrough(OrderLineItem::class, Product::class);
}
public function products(): HasManyThrough
{
return $this->hasManyThrough(OrderLineItem::class, Product::class);
}
Hiteksha
HitekshaOP5mo ago
What's code for display product in table & filter in Resource file?
gladjanus43
gladjanus435mo ago
I think you can then do something like this in the table of orders: TextColumn::make('products.name') ->listWithLineBreaks() ->bulleted()
Hiteksha
HitekshaOP5mo ago
Its not working and give error like this Do you got proper idea about table connection? It's like : Order is connected with orderLineItem (orderLineItem have order_id & product_id column) and i want to display product name so i need to again connect with product
No description
Dennis Koch
Dennis Koch5mo ago
Sounds like Order hasMany LineItems and LineItem belongsTo Product and Order. So you can try:
TextColumn::make('products')
->getStateUsing(fn (Order $order) => $order->lineItems->map(
fn (OderLineItem $item) => $item->product->name
)),
TextColumn::make('products')
->getStateUsing(fn (Order $order) => $order->lineItems->map(
fn (OderLineItem $item) => $item->product->name
)),
Make sure to lazy load your data:
$table->modifyQuery(fn ($query) => $query->with('lineItems.product')
$table->modifyQuery(fn ($query) => $query->with('lineItems.product')
Hiteksha
HitekshaOP5mo ago
Where i need to put lazy loading code?
Dennis Koch
Dennis Koch5mo ago
Inside the ->table() method of your resource. Code is untested and might need some adjustment
Want results from more Discord servers?
Add your server