Reputation: 21
I am trying to code a custom function that will be called by different actions from multiple custom pages. In the traditional Laravel framework, I would have located these in a Services file under App\Services. However, when I create a Services file with the normal __construct() on the pages file, I get an error that the incorrect number of arguments are being passed.
I created a function in a Service as follows:
<?php
namespace App\Services;
class TripService
{
public function test(){
dd('yes');
}
}
Then, on a custom page called LocationVolumeView, I added the normal construction:
class LocationVolumeView extends Page implements HasForms, HasTable
{
private $tripService;
public function __construct (TripService $tripService)
{
$this->tripService = $tripService;
}
Then, later on the same page, I have an action that calls on this function:
->action(function (array $data,): void {
$this->tripService->test();
})
Unfortunately I get the error:
Too few arguments to function App\Filament\Pages\LocationVolumeView::__construct(), 0 passed in /Users/iainreynard/Code/TMS_ver2/vendor/livewire/livewire/src/Features/SupportPageComponents/SupportPageComponents.php on line 212 and exactly 1 expected
Any help appreciated
Upvotes: 0
Views: 552
Reputation: 21
Thanks to some outside help, the following answer works:
Because a custom page is just a live-wire component, the way to solve this is to use traits. In this way, functions can be added to the page
Useful link: Using Traits in Laravel
Upvotes: 1