首页 > 解决方案 > 数据绑定 Laravel Livewire 数学运算出错

问题描述

我创建了一些运算,例如 sum 或 average 或 math 上的任何东西。但有错误

不支持的操作数类型

只是尝试总结我的输入,但有这样的错误。我的输入是这样的。

 <form>
            <div class="pt-6 md:pt-0 md:pl-2">
              
                <button wire:click.prevent="add({{$i}})"  class="bg-green-500 hover:bg-green-400 text-white font-bold py-2 px-4 border-b-4 border-green-700 hover:border-green-500 rounded">
                    Add 
                </button>
                
            </div>
           
        </div>
        <div class="grid grid-cols-1 md:grid-cols-1 gap-6">
            <div class="grid grid-cols-6 gap-2 border border-gray-200 p-4 rounded">

                <input type="hidden" wire:model="newid.0">
                
                <div class="mb-4">
                    <label class="block text-gray-700 text-sm font-bold mb-2" for="username">
                      Nama Barang
                    </label>
                    <div class="flex border rounded bg-gray-300 items-center p-2 ">
                   
                        <svg class="fill-current text-gray-800 mr-2 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
                            <path class="heroicon-ui"
                                  d="M12 12a5 5 0 1 1 0-10 5 5 0 0 1 0 10zm0-2a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm9 11a1 1 0 0 1-2 0v-2a3 3 0 0 0-3-3H8a3 3 0 0 0-3 3v2a1 1 0 0 1-2 0v-2a5 5 0 0 1 5-5h8a5 5 0 0 1 5 5v2z"/>
                        </svg>
                       
                        <input wire:model="nama_barang.0"
                        type="text" placeholder="Enter text here..."
                               class="bg-gray-300 max-w-full focus:outline-none text-gray-700"/>
                    </div>                      
                </div>

                <div class="mb-4">
                    <label class="block text-gray-700 text-sm font-bold mb-2" for="username">
                      Satuan
                    </label>
                    <div class="flex border rounded bg-gray-300 items-center p-2 ">
                   
                        <svg class="fill-current text-gray-800 mr-2 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
                            <path class="heroicon-ui"
                                  d="M12 12a5 5 0 1 1 0-10 5 5 0 0 1 0 10zm0-2a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm9 11a1 1 0 0 1-2 0v-2a3 3 0 0 0-3-3H8a3 3 0 0 0-3 3v2a1 1 0 0 1-2 0v-2a5 5 0 0 1 5-5h8a5 5 0 0 1 5 5v2z"/>
                        </svg>
                       
                        <input wire:model="satuan.0"
                        type="text" placeholder="Enter text here..."
                               class="bg-gray-300 max-w-full focus:outline-none text-gray-700"/>
                    </div>                      
                </div>

                <div class="mb-4">
                    <label class="block text-gray-700 text-sm font-bold mb-2" for="username">
                      Harga
                    </label>
                    <div class="flex border rounded bg-gray-300 items-center p-2 ">
                   
                        <svg class="fill-current text-gray-800 mr-2 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
                            <path class="heroicon-ui"
                                  d="M12 12a5 5 0 1 1 0-10 5 5 0 0 1 0 10zm0-2a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm9 11a1 1 0 0 1-2 0v-2a3 3 0 0 0-3-3H8a3 3 0 0 0-3 3v2a1 1 0 0 1-2 0v-2a5 5 0 0 1 5-5h8a5 5 0 0 1 5 5v2z"/>
                        </svg>
                       
                        <input wire:model="harga"
                        type="number" placeholder="Enter text here..."
                               class="bg-gray-300 max-w-full focus:outline-none text-gray-700"/>
                    </div>                      
                </div>

                <div class="mb-4">
                    <label class="block text-gray-700 text-sm font-bold mb-2" for="username">
                      Jumlah
                    </label>
                    <div class="flex border rounded bg-gray-300 items-center p-2 ">
                   
                        <svg class="fill-current text-gray-800 mr-2 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
                            <path class="heroicon-ui"
                                  d="M12 12a5 5 0 1 1 0-10 5 5 0 0 1 0 10zm0-2a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm9 11a1 1 0 0 1-2 0v-2a3 3 0 0 0-3-3H8a3 3 0 0 0-3 3v2a1 1 0 0 1-2 0v-2a5 5 0 0 1 5-5h8a5 5 0 0 1 5 5v2z"/>
                        </svg>
                       
                        <input wire:model="qty"
                        type="number" placeholder="Enter text here..."
                               class="bg-gray-300 max-w-full focus:outline-none text-gray-700"/>
                    </div>                      
                </div>

                <div class="mb-4">
                    <label class="block text-gray-700 text-sm font-bold mb-2" for="username">
                      Total
                    </label>
                    <div class="flex border rounded bg-gray-300 items-center p-2 ">
                   
                        <svg class="fill-current text-gray-800 mr-2 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
                            <path class="heroicon-ui"
                                  d="M12 12a5 5 0 1 1 0-10 5 5 0 0 1 0 10zm0-2a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm9 11a1 1 0 0 1-2 0v-2a3 3 0 0 0-3-3H8a3 3 0 0 0-3 3v2a1 1 0 0 1-2 0v-2a5 5 0 0 1 5-5h8a5 5 0 0 1 5 5v2z"/>
                        </svg>
                       
                        <input 
                        type="text" wire:model="total" placeholder="Enter text here..."
                               class="bg-gray-300 max-w-full focus:outline-none text-gray-700"/>
                    </div>                      
                </div>

                <div class="mb-4">
                    <label class="block text-white text-sm font-bold mb-2" for="username">
                        Remove
                    </label>
                    <button disabled  class="bg-red-500 hover:bg-red-400 text-white font-bold py-2 px-4 border-b-4 border-red-700 hover:border-red-500 rounded">
                        Hapus
                    </button>           
                </div>

                @foreach($inputs as $key => $value)

                <input type="hidden" wire:model="newid.{{ $value }}">

                <div class="mb-4">
                   
                    <div class="flex border rounded bg-gray-300 items-center p-2 ">
                   
                        <svg class="fill-current text-gray-800 mr-2 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
                            <path class="heroicon-ui"
                                  d="M12 12a5 5 0 1 1 0-10 5 5 0 0 1 0 10zm0-2a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm9 11a1 1 0 0 1-2 0v-2a3 3 0 0 0-3-3H8a3 3 0 0 0-3 3v2a1 1 0 0 1-2 0v-2a5 5 0 0 1 5-5h8a5 5 0 0 1 5 5v2z"/>
                        </svg>
                       
                        <input  wire:model="nama_barang.{{ $value }}"
                        type="text" placeholder="Enter text here..."
                        class="bg-gray-300 max-w-full focus:outline-none text-gray-700"/>
                    </div>                      
                </div>

                <div class="mb-4">
                   
                    <div class="flex border rounded bg-gray-300 items-center p-2 ">
                   
                        <svg class="fill-current text-gray-800 mr-2 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
                            <path class="heroicon-ui"
                                  d="M12 12a5 5 0 1 1 0-10 5 5 0 0 1 0 10zm0-2a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm9 11a1 1 0 0 1-2 0v-2a3 3 0 0 0-3-3H8a3 3 0 0 0-3 3v2a1 1 0 0 1-2 0v-2a5 5 0 0 1 5-5h8a5 5 0 0 1 5 5v2z"/>
                        </svg>
                       
                        <input   wire:model="satuan.{{ $value }}"
                        type="text" placeholder="Enter text here..."
                               class="bg-gray-300 max-w-full focus:outline-none text-gray-700"/>
                    </div>                      
                </div>

                <div class="mb-4">
                    
                    <div class="flex border rounded bg-gray-300 items-center p-2 ">
                   
                        <svg class="fill-current text-gray-800 mr-2 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
                            <path class="heroicon-ui"
                                  d="M12 12a5 5 0 1 1 0-10 5 5 0 0 1 0 10zm0-2a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm9 11a1 1 0 0 1-2 0v-2a3 3 0 0 0-3-3H8a3 3 0 0 0-3 3v2a1 1 0 0 1-2 0v-2a5 5 0 0 1 5-5h8a5 5 0 0 1 5 5v2z"/>
                        </svg>
                       
                        <input  wire:model="harga.{{ $value }}"
                        type="text" placeholder="Enter text here..."
                               class="bg-gray-300 max-w-full focus:outline-none text-gray-700"/>
                    </div>                      
                </div>

                <div class="mb-4">
                   
                    <div class="flex border rounded bg-gray-300 items-center p-2 ">
                   
                        <svg class="fill-current text-gray-800 mr-2 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
                            <path class="heroicon-ui"
                                  d="M12 12a5 5 0 1 1 0-10 5 5 0 0 1 0 10zm0-2a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm9 11a1 1 0 0 1-2 0v-2a3 3 0 0 0-3-3H8a3 3 0 0 0-3 3v2a1 1 0 0 1-2 0v-2a5 5 0 0 1 5-5h8a5 5 0 0 1 5 5v2z"/>
                        </svg>
                       
                        <input  wire:model="qty.{{ $value }}"
                        type="text" placeholder="Enter text here..."
                               class="bg-gray-300 max-w-full focus:outline-none text-gray-700"/>
                    </div>                      
                </div>

                <div class="mb-4">
                    
                    <div class="flex border rounded bg-gray-300 items-center p-2 ">
                   
                        <svg class="fill-current text-gray-800 mr-2 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
                            <path class="heroicon-ui"
                                  d="M12 12a5 5 0 1 1 0-10 5 5 0 0 1 0 10zm0-2a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm9 11a1 1 0 0 1-2 0v-2a3 3 0 0 0-3-3H8a3 3 0 0 0-3 3v2a1 1 0 0 1-2 0v-2a5 5 0 0 1 5-5h8a5 5 0 0 1 5 5v2z"/>
                        </svg>
                       
                        <input 
                        type="text" placeholder="Enter text here..."
                               class="bg-gray-300 max-w-full focus:outline-none text-gray-700"/>
                    </div>                      
                </div>

                <div class="mb-4">
                   
                    <button wire:click.prevent="remove({{$key}})" class="bg-red-500 hover:bg-red-400 text-white font-bold py-2 px-4 border-b-4 border-red-700 hover:border-red-500 rounded">
                        Hapus
                    </button>           
                </div>

             



                @endforeach




               
              
            </div>



        </div>
        <div  class="flex justify-left"><button wire:click.prevent="store()" class="p-2 border w-1/4 rounded-md bg-gray-800 text-white">Submit</button></div>
    </form>

我的功能

public $belanja_id, $nama_barang, $satuan,$newid;
public $updateMode = false;
public $inputs = [];
public $i = 1;
public $total ;
public $harga = [] ;
public $qty = [];

public function add($i)
{
    $i = $i + 1;
    $this->i = $i;
    array_push($this->inputs ,$i);
}

public function mount($id)
{
   $belanja = $this->belanja = Belanja::findOrFail($id);
   $this->newid = $belanja->id;
   

} 

public function remove($i)
{
    unset($this->inputs[$i]);
}

public function render()
{
    $this->total = $this->harga + $this->qty;

    return view('livewire.input-belanja-lw');
}

更新此输入。我在这个字段输入上使用了多个输入,这个多个如何可以使用带有数学运算的操作数据绑定?. 有人可以解决吗?

标签: laravellaravel-livewire

解决方案


推荐阅读