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!');
}
}
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!');
}
@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);
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');
}
}
repository pattern isn't applicable with eloquent (massive duplication and wasted effort)
@Snapey can you explain a little bit why
@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.
@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 sign in or create an account to participate in this conversation.