如何将来自控制器刀片的数据发送到组件LiveWire

发布于 2025-01-23 13:21:44 字数 2804 浏览 0 评论 0原文

认识Livewire-laravel的人我需要在Livewire上使用一些代码(表单更新) 我正在尝试传递数据(产品) :来自控制器(布局内容) 对于LiveWire组件(产品更高),

但它不起作用,有时它无法识别“ $产品”,有时它会发送表格不起作用,从Livewire组件中起作用

,那么什么是好习惯?要检索产品数据并使从LiveWire的表单更新工作,

请预先

致电LiveWire组件(由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'));

    }

我的组件(Update-Produit)in order以测试提交的提交, 但是表格不会提交!!!它重新加载并返回URL中的令牌:

@extends('Admin.layoutAdmin')
@section('content')
{{-- <livewire:update-produit :produits="$produits")> --}}
<div>
    <div class="container-fluid mb-5">
        <div class="row mb-4">
            <div class="col-lg-10">
                <h3> Modifier Produit </h3>
            </div>   
        </div>
        <div class="container">
            <form class="contact-form row" wire:submit.prevent="oui" enctype = "multipart/form-data">                       
                @csrf
                <input type="text" wire:model="oui">
                <div class="container-fluid d-flex align-items-center justify-content-center" >                         
                    <button wire:click="submit" type="submit" class="btn btn-primary">Enregistrer</button>

                </div>
            </form>

        </div>
    </div>
</div>

LiveWire功能pour pour test表格提交:

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);

    }

请帮助我!

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 class="container-fluid mb-5">
        <div class="row mb-4">
            <div class="col-lg-10">
                <h3> Modifier Produit </h3>
            </div>   
        </div>
        <div class="container">
            <form class="contact-form row" wire:submit.prevent="oui" enctype = "multipart/form-data">                       
                @csrf
                <input type="text" wire:model="oui">
                <div class="container-fluid d-flex align-items-center justify-content-center" >                         
                    <button wire:click="submit" type="submit" class="btn btn-primary">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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小姐丶请自重 2025-01-30 13:21:45

每当使用Livewire时,我都不会碰到Laravel控制器,因为它有自己的课程。如果要检索产品数据,只需制作一个LiveWire组件并将其通过视图即可。这样的东西(对不起)

productIndex.php

...
protected $listeners = ['productUpdated'];

public function productUpdated() {
  // Just Blank
}

public function render() {
  return view('your-livewire-product-index', ['products' => Product::all());
}

刀片产品索引

<div>
  <ul>
     @foreach($products as $product) {
       <li>{{ $product->name }} | <a href="{{ route('products.edit', ['product' => $product->id]) }}">Edit</a></li>
     @endforeach
  </ul>
</div>

此用于更新

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 call event so ProductIndex listen it and it will auto update the productIndex list
   $this->emit('productUpdated');
}

public function render() {
   return view('livewire-edit-product', ['product' => Product::find($this->product_id)]);
}

blade blade 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>

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 call event so ProductIndex listen it and it will auto update the productIndex list
   $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>
归途 2025-01-30 13:21:45

您应该在控制器视图(刀片文件)中调用LiveWire组件,并通过它传递数据(产品)。

You should call the LiveWire component in the controller view (blade file) and pass the data (products) through it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文