Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anonymouse703's avatar

Is Repository Pattern is applicable in Livewire Component?

I noticed that when I used the repository pattern through mount it will only working in render but not in other function

<?php

namespace App\Livewire\Sample;

use App\Repositories\Contracts\SampleRepositoryInterface;
use Livewire\Component;

class Index extends Component
{

    protected $sampleRepository;

    public function mount(SampleRepositoryInterface $sampleRepository)
    {
        $this->sampleRepository = $sampleRepository;
    }


    public function render()
    {
        dd($this->sampleRepository);

        $datas = $this->sampleRepository->paginated();

        return view('livewire.sample.index', ['datas' => $datas]);
    }

    public function removeSample($id)
    {
        dd($this->sampleRepository);

        $post = $this->sampleRepository->find($id);

        if ($post) {
            $post->delete();
        }

        return redirect()->to('/sample')->with('status', 'Sample deleted!');
    }
}
0 likes
8 replies
anonymouse703's avatar

I got it...

 public function save(SampleRepositoryInterface $sampleRepository)
    {
        $validatedData = Validator::make($this->formData, (new StoreRequest)->rules())->validate();

        $sampleRepository->create($validatedData);

        return redirect()->to('/sample')->with('status', 'Post created!');
    }
MohamedTammam's avatar

@anonymouse703 This is unsafe. The user can pass different values to this function.

Use this instead

public function save()
{
        $sampleRepository = new SampleRepositoryInterface();
        $validatedData = Validator::make($this->formData, (new StoreRequest)->rules())->validate();

        $sampleRepository->create($validatedData);

        return redirect()->to('/sample')->with('status', 'Post created!');
}

OR

$sampleRepository = app(SampleRepositoryInterface::class);
anonymouse703's avatar

Like this?

<?php

namespace App\Livewire\Sample;

use Livewire\Component;
use Livewire\Attributes\On;
use Illuminate\Support\Facades\Validator;
use App\Http\Requests\Admin\Sample\StoreRequest;
use App\Repositories\Contracts\SampleRepositoryInterface;

class Create extends Component
{

    public $formData = [
        'id' => '',
        'name' => '',
        'address' => ''
    ];

    public function close()
    {
        $this->dispatch('close-modal');
    }


    public function save()
    {
        $sampleRepository = app(SampleRepositoryInterface::class);

        $validatedData = Validator::make($this->formData, (new StoreRequest)->rules())->validate();

        $sampleRepository->create($validatedData);

        return redirect()->to('/sample')->with('status', 'Post created!');
    }

  
    #[On('edit-sample')]          
    public function edit(int $id) 
    {
        $sampleRepository = app(SampleRepositoryInterface::class);


        $sample = $sampleRepository->find($id);

        $this->formData = [
            'id' => $sample->id,
            'name' => $sample->name,
            'address' => $sample->address,
          ];

        $this->dispatch('open-modal');
    }

    public function render()
    {
        return view('livewire.sample.create');
    }

}
Snapey's avatar

repository pattern isn't applicable with eloquent (massive duplication and wasted effort)

Snapey's avatar

@beratmasat-120595280 in general, repository pattern duplicates a lot of eloquent functionality so it is little used in Laravel projects.

In Livewire specifically, Livewire does not know how to rehydrate your repository object on each render cycle.

1 like
beratmasat-dev's avatar

@Snapey yes, I realized this, I'm currently developing a small project with livewire and as you mentioned, at this point, the repository pattern doesn't change much, but I think long queries can be skipped with scopes.

Please or to participate in this conversation.