9

I've got a piece of code I want to reuse. I've read this Laravel cleaner code article and this other Laravel Services Pattern article, where I have realized I can reuse code in several places of the application by using services classes.

In this case, I created a new MyService class, inside a new folder app/Services/MyService.

namespace App\Services;

class MyService
{
    public function reuse_code($param){
       return void;
    }
}

The problem comes when I want to call the class through the constructor inside a Livewire class component, as follows:

<?php

namespace App\Http\Livewire;

use App\Services\MyService;
use Livewire\Component;
use Livewire\WithPagination;

class LivewireTable extends Component
{
    use WithPagination;

    private $myClassService;

    public function __construct(MyService $myService)
    {
        $this->myClassService = $myService;
    }

    public function render()
    {
       $foo = $this->myClassService->reuse_code($param);
       return view('my.view',compact('foo'));
    }
}

The error displayed is the following:

Argument 1 passed to App\Http\Livewire\LivewireTable::__construct() must be an instance of App\Services\MyService, string given

(However, If I use a trait, there are no problems. But I am afraid then my traits collide as previous experiences)

How do I fix it? What am I missing?

Pathros
  • 10,042
  • 20
  • 90
  • 156
  • 5
    how are you calling the Livewire class? Have you tried using `mount()` instead of `__construct()`? – IGP Jan 26 '21 at 19:54
  • Indeed, [in the docs](https://laravel-livewire.com/docs/2.x/rendering-components#parameters) it says so! That did the trick. Thx a lot! – Pathros Jan 26 '21 at 20:24

2 Answers2

8

Livewire's boot method Runs on every request, immediately after the component is instantiated, but before any other lifecycle methods are called

Here's the solution worked for me.

public bool $checkAllRecords = false;

public array $checkedRecords = [];

private MailService $service;

public function boot(MailInterface $service)
{
    $this->service = $service;
}

public function updatingCheckAllRecords($value)
{
    $this->checkRecords = $value ? (array) $this->service->getListData($this->perPage, $this->search)->pluck('id') : []
}
sh6210
  • 4,190
  • 1
  • 37
  • 27
6

Solved Just like @IGP said, reading in the livewire docs it says:

In Livewire components, you use mount() instead of a class constructor __construct() like you may be used to.

So, my working code is as follows:

    <?php
    
    namespace App\Http\Livewire;
    
    use App\Services\MyService;
    use Livewire\Component;
    use Livewire\WithPagination;
    
    class LivewireTable extends Component
    {
        use WithPagination;
    
        private $myClassService;
    
        public function mount(MyService $myService)
        {
            $this->myClassService = $myService;
        }
    
        public function render()
        {
           $foo = $this->myClassService->reuse_code($param);
           return view('my.view',compact('foo'));
        }
    }
Pathros
  • 10,042
  • 20
  • 90
  • 156