Insert multiple value laravel

huangapple go评论55阅读模式
英文:

Insert multiple value laravel

问题

你的代码在Controller中似乎只插入了nama_menu的一个值。如果你希望从View Blade中插入多个nama_menu值,你可以将这些值存储为一个数组,然后在Controller中进行处理。以下是一种可能的修改方法:

在View Blade中,使用一个数组来存储多个nama_menu值:

@foreach($fi as $z)
    <input type="hidden" value="{{$z->name}}" name="nama_menu[]" id="nama_menu">
    <input type="hidden" value="{{$z->quantity}}" name="jumlah[]" id="jumlah">
    <input type="hidden" value="{{$z->price * $z->quantity}}" name="total_pembayaran[]" id="total">
@endforeach

在Controller中,处理这些数组并插入多个记录:

public function store(Request $r){
    $nama_menu = $r->input('nama_menu');
    $jumlah = $r->input('jumlah');
    $total_pembayaran = $r->input('total_pembayaran');

    // 假设这些数组具有相同的长度,你可以使用循环来插入多个记录
    for ($i = 0; $i < count($nama_menu); $i++) {
        $history = new History();
        $history->nama = $r->nama;
        $history->provinsi = $r->provinsi;
        $history->nama_menu = $nama_menu[$i];
        $history->jumlah = $jumlah[$i];
        $history->kota = $r->kota;
        $history->total_pembayaran = $total_pembayaran[$i];
        // 插入其他字段

        $history->save();
    }

    return redirect(url('/checkout'))->with(['success' => 'berhasil mengirim pesan whatsapp']);
}

这样,你的Controller会根据数组中的值插入多个记录,每个记录都包含不同的nama_menu值。

英文:

Hello i want insert multiple value in nama_menu

My Code In Controller

 public function store(Request $r){
        $history = new History();
        $history-&gt;nama = $r-&gt;nama;
        $history-&gt;provinsi = $r-&gt;provinsi;
        $history-&gt;nama_menu = $r-&gt;nama_menu;
        $history-&gt;jumlah = $r-&gt;jumlah;
        $history-&gt;kota = $r-&gt;kota;
        $history-&gt;total_pembayaran = $r-&gt;total_pembayaran;
        $history-&gt;status = $r-&gt;status;
        $history-&gt;kecamatan = $r-&gt;kecamatan;
        $history-&gt;kelurahan = $r-&gt;kelurahan;
        $history-&gt;kodepos = $r-&gt;kodepos;
        $history-&gt;address = $r-&gt;address;
        $history-&gt;whatsapp = $r-&gt;whatsapp;
        $history-&gt;jenis_pembayaran = $r-&gt;jenis_pembayaran;
        $history-&gt;save();
        
        return redirect(url(&#39;/checkout&#39;))-&gt;with([&#39;success&#39; =&gt; &#39;berhasil mengirim pesan whatsapp&#39;]);;
    }

My View Blade
` <div class="cart-page-heading">
<h5>Orderan Kamu</h5>
<p>The Details</p>
</div>

                    &lt;ul class=&quot;order-details-form mb-4&quot;&gt;
                        &lt;li&gt;&lt;span&gt;Product&lt;/span&gt;&lt;span&gt;Jumlah&lt;/span&gt; &lt;span&gt;Total&lt;/span&gt;&lt;/li&gt;
                        @foreach($fi as $z)
                        &lt;input type=&quot;hidden&quot; value=&quot;{{$z-&gt;name}}&quot; name=&quot;nama_menu&quot; id=&quot;nama_menu&quot;&gt;
                        &lt;input type=&quot;hidden&quot; value=&quot;{{$z-&gt;quantity}}&quot; name=&quot;jumlah&quot; id=&quot;jumlah&quot;&gt;
                        &lt;input type=&quot;hidden&quot; value=&quot;{{$z-&gt;price * $z-&gt;quantity}}&quot; name=&quot;total_pembayaran&quot; id=&quot;total&quot;&gt;

                        &lt;li&gt;&lt;span&gt;{{$z-&gt;name}}&lt;/span &gt;&lt;span&gt;{{$z-&gt;quantity}}&lt;/span&gt; &lt;span&gt;Rp. {{($z-&gt;price * $z-&gt;quantity)}}&lt;/span&gt;&lt;/li&gt;
                        @endforeach
                        &lt;li&gt;&lt;span&gt;Total&lt;/span&gt; &lt;span&gt;Rp. {{ Cart::getTotal() }}&lt;/span&gt;&lt;/li&gt;
                    &lt;/ul&gt;

`

when i insert the data only nama_menu show one value
insert fieled

i want nama_menu insert many viewbladevalues from view blade

答案1

得分: 0

convert into an array or comma-separated value. it possible way. after that used to implode and explode functions in PHP

英文:

convert into an array or comma-separate value. it possible way. after that used to implode and explode functions in PHP

huangapple
  • 本文由 发表于 2023年4月4日 11:26:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定