Issues with Attach/Associate action between a belongsTo/hasMany relationship

Hi everyone, I have a model called Order and another called OrderStatus. The relevant realtionships are the following:
Order.php

public function orderStatus()
{
return $this->belongsTo(OrderStatus::class);
}
Order.php

public function orderStatus()
{
return $this->belongsTo(OrderStatus::class);
}
OrderStatus.php

public function order()
{
return $this->hasMany(Order::class);
}
OrderStatus.php

public function order()
{
return $this->hasMany(Order::class);
}
I created an OrderStatusRelationManager in OrderResource and registered it in getRelations. I want to be able to change the order status. In my the table function I have the following
->headerActions([
Tables\Actions\AssociateAction::make()->preloadRecordSelect(),
])
->headerActions([
Tables\Actions\AssociateAction::make()->preloadRecordSelect(),
])
I'm getting the following error:
Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::associate()
Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::associate()
I also tried AttachAction in case I misunderstood which one I should use and I got
Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::getRelatedKeyName()
Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::getRelatedKeyName()
I also defined the inverse relation in my relation manager.
protected static ?string $inverseRelationship = 'orders';
protected static ?string $inverseRelationship = 'orders';
What am I missing ?
2 Replies
cheesegrits
cheesegrits16mo ago
I'm confused as to why you are trying to use a relation manager for this. Surely just having a ...
Select::('order_status_id')
relationship('orderStatus', 'status')
Select::('order_status_id')
relationship('orderStatus', 'status')
... field on your Order form is all you need? The relationship from Order to OrderStatus is a BelongsTo, so a relation manager for it won't work. RM's are only for "many" relations. So you could have an RM on your OrderStatus resource for Orders, showing all the orders belonging to a given status and associate/dissociate them from there. But on the Order resource, it's just a simple one-to-one BelongsTo, so there is no attachable/associatable relationship.
guitarnerd_
guitarnerd_OP16mo ago
Oh ok thanks
Want results from more Discord servers?
Add your server