F
Filament11mo ago
Zod

How do I load a model function before viewing the record?

I am viewing a record but I want to mutate the data with a function that is defined in my model. How do I do this?
8 Replies
Zod
ZodOP11mo ago
I am showing it in a custom view
LeandroFerreira
LeandroFerreira11mo ago
share the code
Zod
ZodOP11mo ago
This is my function in my Company model: public function calculateRevenue() { return $this->orders() ->where('paid', true) ->selectRaw('sum(price) as sum, YEAR(created_at) as year, MONTH(created_at) as month') ->groupBy('year', 'month') ->get(); } I want to define $record->revenue = $record->calculateRevenue() somewhere Before loading the view mutate function in the ViewCompany.php will only get me an array and not the model.
LeandroFerreira
LeandroFerreira11mo ago
are you using a custom view field? What are you rendering in the view? you can access {{ $getRecord()->field }} inside the view
Zod
ZodOP11mo ago
But it's not a field yet It s a function in my model I am doing a $record->revenue->groupBy in my view
LeandroFerreira
LeandroFerreira11mo ago
You could use an accessor to customize an attribute https://laravel.com/docs/10.x/eloquent-mutators#defining-an-accessor
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Zod
ZodOP11mo ago
Thats probably the way I would go then Thanks for that mate
Want results from more Discord servers?
Add your server