People who know LIVEWIRE-LARAVEL ‼ I need to get some code working on livewire ( form update ) I'm trying to pass data (products) :From controller ( layout content ) To a livewire component (product-update)
But it doesn't work, sometimes it doesn't recognize "$products" sometimes it sends forms doesn't work from livewire component
So what is good practice? To retrieve product data and make form update work from livewire
Thanks in advance
controller that call livewire component (as suugested by KeyvanGholami):
public function editProduit($idsscatg, $idprd){
$ss_catg = SousCategorie::find($idsscatg);
$idsscatgg = $idsscatg;
$idprdd = $idprd;
$nom_catg = $ss_catg->nom_catg;
if( $ss_catg->nom_catg == 'Consommable médico-chirurgical')
{
$produit = ProduitConsommable::find($idprdd);
return view('livewire.update-produit',compact('produit', 'idsscatgg', 'nom_catg'));
}
my component (update-produit) inorder to test the submiting for, BUT THE FORM DOENS'T SUBMIT !!! it reload and return back the form with the token in url:
@extends('Admin.layoutAdmin')
@section('content')
{{-- <livewire:update-produit :produits="$produits")> --}}
<div>
<div >
<div >
<div >
<h3> Modifier Produit </h3>
</div>
</div>
<div >
<form wire:submit.prevent="oui" enctype = "multipart/form-data">
@csrf
<input type="text" wire:model="oui">
<div >
<button wire:click="submit" type="submit" >Enregistrer</button>
</div>
</form>
</div>
</div>
</div>
livewire function pour test form submit:
public function mount(){
$cons = SousCategorie::where('nom_catg', 'Consommable médico-chirurgical')->get();
$equips = SousCategorie::where('nom_catg', 'Equipements')->get();
$mobs = SousCategorie::where('nom_catg', 'Mobilier médical')->get();
$landangers = SousCategorie::where('nom_catg', 'Landanger')->get();
$delacroixs = SousCategorie::where('nom_catg', 'Delacroix-chevalier')->get();
$divs = SousCategorie::where('nom_catg', 'Divers')->get();
View::share('cons', $cons);
View::share('equips', $equips);
View::share('mobs', $mobs);
View::share('landangers', $landangers);
View::share('delacroixs', $delacroixs);
View::share('divs', $divs);
}
public function render($produits)
{
return view('livewire.update-produit');
}
public function oui(){
$oui = $this->oui;
dd($oui);
}
Help me please !
CodePudding user response:
You should call the LiveWire component in the controller view (blade file) and pass the data (products) through it.
CodePudding user response:
I never touch laravel controller whenever using livewire since it has their own Class for that. If you want to retrieve Product data, Just make a livewire component and Pass it through view. Something like this (sorry its horrible)
ProductIndex.php
...
protected $listeners = ['productUpdated'];
public function productUpdated() {
// Just Blank
}
public function render() {
return view('your-livewire-product-index', ['products' => Product::all());
}
Blade product Index
<div>
<ul>
@foreach($products as $product) {
<li>{{ $product->name }} | <a href="{{ route('products.edit', ['product' => $product->id]) }}">Edit</a></li>
@endforeach
</ul>
</div>
This for update
ProductEdit.php
...
public $name, $product_id;
public Product $product;
public function mount() {
$getProductID = explode('/', url()->current());
$this->product_id = end($getProductID);
}
public function update() {
// For simplicity i just update product name
Product::find($this->product_id)->update(['name' => $this->name]);
// I
$this->emit('productUpdated');
}
public function render() {
return view('livewire-edit-product', ['product' => Product::find($this->product_id)]);
}
Blade product Edit
<div>
<form wire:submit.prevent="update">
<input type="hidden" wire:model="product_id">
<input type="text" wire:model="name">
<input type="submit" value="save">
</form>
</div>