Cara post data dari session ke database. contoh kita ambil total harga akhir di halaman checkout

Food Controller

 public function update(Request $request)
    {
        if($request->id and $request->jumlah)
        {
            $cart = session()->get('cart');
            $cart[$request->id]["jumlah"] = $request->jumlah;
            session()->put('cart', $cart);
            session()->flash('success', 'Cart updated successfully');
        }
    }
    public function remove(Request $request)
    {
        if($request->id) {
            $cart = session()->get('cart');
            if(isset($cart[$request->id])) {
                unset($cart[$request->id]);
                session()->put('cart', $cart);
            }
            session()->flash('success', 'Product removed successfully');
        }
    }
    public function transaksi(Request $request)
    {
        Transaksi::create($request->all());
        if($request->id and $request->total_harga) {
            $cart = session()->get('cart');
            if(isset($cart[$request->id])) {
                unset($cart[$request->id]);
                session()->put('cart', $cart);
            }
            session()->flash('success', 'Product removed successfully');
        }


    }

master.blade.php

 $(".update-cart").click(function (e) {
           e.preventDefault();
           var ele = $(this);
            $.ajax({
               url: '{{ url('update-cart') }}',
               method: "patch",
               data: {_token: '{{ csrf_token() }}', id: ele.attr("data-id"), jumlah: ele.parents("tr").find(".jumlah").val()},
               success: function (response) {
                   window.location.reload();
               }
            });
        });
        $(".remove-from-cart").click(function (e) {
            e.preventDefault();
            var ele = $(this);
                $.ajax({
                    url: '{{ url('remove-from-cart') }}',
                    method: "DELETE",
                    data: {_token: '{{ csrf_token() }}', id: ele.attr("data-id")},
                    success: function (response) {
                        window.location.reload();
                    }
                });
        });
        $(".pesan").click(function (e) {
            e.preventDefault();
            var ele = $(this);
                $.ajax({
                    url: '{{ url('transaksi') }}',
                    type: "POST",
                    data: {_token: '{{ csrf_token() }}', id: ele.attr("data-id"), total_harga: ele.find(".total").val()},
                    success: function (response) {
                        window.location.reload();
                    }
                });
            });

checkout.blade.php

@extends('master')

@section('title')
E-Foodies | Checkout
@endsection

@section('content')
<!-- checkout section  -->
<section class="checkout-section spad">
    <div class="container">
      <div class="row">
        <div class="col-lg-8 order-2 order-lg-1">
          <form class="checkout-form">
            <div class="cf-title">Billing Address</div>
            <div class="row">
              <div class="col-md-7">
                <p>*Billing Information</p>
              </div>
              <div class="col-md-5">
                <div class="cf-radio-btns address-rb">
                  <div class="cfr-item">
                    <input type="radio" name="pm" id="one">
                    <label for="one">Use my regular address</label>
                  </div>
                  <div class="cfr-item">
                    <input type="radio" name="pm" id="two">
                    <label for="two">Use a different address</label>
                  </div>
                </div>
              </div>
            </div>
            <div class="row address-inputs">
              <div class="col-md-12">
                <input type="text" placeholder="Address">
                <input type="text" placeholder="Address line 2">
                <input type="text" placeholder="Country">
              </div>
              <div class="col-md-6">
                <input type="text" placeholder="Zip code">
              </div>
              <div class="col-md-6">
                <input type="text" placeholder="Phone no.">
              </div>
            </div>
            <div class="cf-title">Delievery Info</div>
            <div class="row shipping-btns">
              <div class="col-6">
                <h4>Standard</h4>
              </div>
              <div class="col-6">
                <div class="cf-radio-btns">
                  <div class="cfr-item">
                    <input type="radio" name="shipping" id="ship-1">
                    <label for="ship-1">Free</label>
                  </div>
                </div>
              </div>
              <div class="col-6">
                <h4>Next day delievery  </h4>
              </div>
              <div class="col-6">
                <div class="cf-radio-btns">
                  <div class="cfr-item">
                    <input type="radio" name="shipping" id="ship-2">
                    <label for="ship-2">$3.45</label>
                  </div>
                </div>
              </div>
            </div>
            <div class="cf-title">Payment</div>
            <ul class="payment-list">
              <li>Paypal<a href="#"><img src="img/paypal.png" alt=""></a></li>
              <li>Credit / Debit card<a href="#"><img src="img/mastercart.png" alt=""></a></li>
              <li>Pay when you get the package</li>
            </ul>
            <button class="site-btn submit-order-btn">Place Order</button>
          </form>
        </div>
        <div class="col-lg-4 order-1 order-lg-2">
          <div class="checkout-cart">
            <h3>Your Cart</h3>
            <table>
              <thead>
              </thead>
              <tbody>
                <?php $total = 0 ?>
              @if(session('cart'))
                @foreach(session('cart') as $id => $details)

                  <?php $total += $details['harga'] * $details['jumlah']  ?>
                <tr>
                  <td>
                    <img src="{{ asset('img/product/'.$details['foto_makanan'])}}"class="rounded mb-3" width="100px" height="100"alt="">
                  </td>
                  <td>
                  <div class="pc-title ml-5">
                      <h5 class="mb-1">{{$details['nama_makanan']}}</h5>
                      <h6>Jumlah : {{$details['jumlah']}}</h6>
                      <h6 class="harga">total harga : {{$details['harga'] * $details['jumlah']. '.000'}}</h6>
                    </div>
                  </td>
                </tr>
                @endforeach
              @endif
              </tbody>
            </table>
            <ul class="price-list">
              <li class="total"name="total">Total : {{$total. ".000"}}</li>
              <td class="actions"><button class="btn-danger btn-sm ml-5 pesan"data-id="{{$id}}">Pesan</button></td>
            </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
  </section>
  <!-- checkout section end -->
@endsection
avatar rizziqibrahim_google
@rizziqibrahim_google

3 Kontribusi 0 Poin

Diperbarui 4 tahun yang lalu

1 Jawaban:

<div>Coba buat input type hidden berisikan value session yang diinginkan yang nantinya akan ikut tersubmit di formnya.<br>Misal :&nbsp;</div><pre>&lt;input type="hidden" value="{{$session}}" name="session"&gt;</pre>

avatar maswid
@maswid

18 Kontribusi 5 Poin

Dipost 4 tahun yang lalu

Login untuk ikut Jawaban