Accessing accessor trough a relationship

Hi, i got a problem and i couldnt find the answer on the net. in my Invoice resource i have a table column like this
TextColumn::make('lineItems.total_vat')
TextColumn::make('lineItems.total_vat')
in the invoice model i have a relationship like this
class Order2CashInvoice extends Model
{
public function lineItems()
{
return $this->hasMany(Order2CashLineItem::class, 'invoice_id');
}
class Order2CashInvoice extends Model
{
public function lineItems()
{
return $this->hasMany(Order2CashLineItem::class, 'invoice_id');
}
And then in my LineItem model i have this
public function getTotalVatAttribute($key = 'BedragBtw')
{
return $this->additionalItemProperties()
->where('name', $key)
->first()
->value ?? null; // Return the value or null if not found
}
public function getTotalVatAttribute($key = 'BedragBtw')
{
return $this->additionalItemProperties()
->where('name', $key)
->first()
->value ?? null; // Return the value or null if not found
}
the additionalitemproperties is a relationship to a model that has many records beloning to one lineitem
class Order2CashAdditionalItemProperty extends Model
{
public function lineItem()
{
return $this->belongsTo(Order2CashLineItem::class, 'line_item_id');
}
class Order2CashAdditionalItemProperty extends Model
{
public function lineItem()
{
return $this->belongsTo(Order2CashLineItem::class, 'line_item_id');
}
i cant figure it out thanks for the help
Solution:
I fixed it using the $state property. It was too much work so just went trough the tables manually
Jump to solution
7 Replies
mvenghaus
mvenghaus9mo ago
hey .. since you're using hasMany .. from which of the many should the total_vat be taken?
Zakhaev
ZakhaevOP9mo ago
Hey thanks for to reply. Initially its a check for the additionalLineItems model. So the additionalLineItems model has multiple rows connected to a line item. This is how its stored. In additionalLineItems. Total=100 TotalVat=50 And in my invoice resource, that has a lineitem. I want to display a column in the table giving me the totalVat. Its a bit hard to explain but i hope im explaining it good I could provide screenshots if needed
mvenghaus
mvenghaus9mo ago
mmh i really don't get it 🙂 but for my understanding .. if you use hasMany .. and want to use it in a column .. you have either decide which of the many like ->latestOfMany() or you have to aggregate ->hasOne(LineItem::class)->ofMany('total_vat', 'sum')
Zakhaev
ZakhaevOP9mo ago
Alright thank you for your answer. But how would i make a textcolumn from a eloquentaccessor. Maybe that is easier to explain. I just want to use a field that is filtered thats why im using a accessor
mvenghaus
mvenghaus9mo ago
what exactly do you want to filter ? if you don't use s.th. like latestOfMany .. eloquent load the data using different select .. if you want to filter or sort .. u need a join on the tables
Solution
Zakhaev
Zakhaev9mo ago
I fixed it using the $state property. It was too much work so just went trough the tables manually
Zakhaev
ZakhaevOP9mo ago
Thanks anyway💪
Want results from more Discord servers?
Add your server