Question:
Laravel Error Solved: compact(): Undefined variable $pesanan_details

Problem

My error

compact(): Undefined variable $pesanan_details

Line 138 experienced an error and a red notification on that line

return view('checkout.check_out', compact('pesanan','pesanan_details'));


Controller controller/PesananController


<?php


namespace App\Http\Controllers;


use App\Models\Pesanan;

use App\Models\Post;

use App\Models\PesananDetail;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

use App\Http\Controllers\Controller;

use App\Http\Requests\StorePesananRequest;

use App\Http\Requests\UpdatePesananRequest;


class PesananController extends Controller

{

    /**

     * Display a listing of the resource.

     */

    public function __construct()

    {

        $this->middleware('auth');

    }


    public function index()

    {

        $post = post::where('id', $id)->first();

        return view('pesan.index', compact('barang'));

    }


    /**

     * Show the form for creating a new resource.

     */

    public function create()

    {

        //

    }


    /**

     * Store a newly created resource in storage.

     */

    public function store(StorePesananRequest $request)

    {

        //

    }


    /**

     * Display the specified resource.

     */

    public function show(Pesanan $pesanan)

    {

        //

    }


    /**

     * Show the form for editing the specified resource.

     */

    public function edit(Pesanan $pesanan)

    {

        //

    }


    /**

     * Update the specified resource in storage.

     */

    public function update(UpdatePesananRequest $request, Pesanan $pesanan)

    {

        //

    }


    /**

     * Remove the specified resource from storage.

     */

    public function destroy(Pesanan $pesanan)

    {

        //

    }


    public function pesan(Request $request, $id)

    {

        // dd($request);

        $post = Post::where('id', $id)->first();


        // cek validasi pesanan melebihi stok

        // if($request->jumlah_pesan > $post->stok)

        // {

        //     return redirect("/post")->with('No', 'Stok Kurang Dari Pesanan Anda');

        // }

        // cek validasi pesanan

        $cek_pesanan = Pesanan::where('user_id', Auth::user()->id)->where('status',0)->first();

        // simpan  ke pesanan

        if(empty($cek_pesanan))

        {

            $pesanan = new Pesanan;

            $pesanan->user_id = Auth::user()->id;

            $pesanan->status = 0;

            $pesanan->jumlah_harga = 0;

            $pesanan->save();

        }


        $pesanan_baru = Pesanan::where('user_id', Auth::user()->id)->where('status',0)->first();


        // cek pesanan_detail

        $cek_pesanan_detail = PesananDetail::where('post_id', $post->id)->where('pesanan_id', $pesanan_baru->id)->first();

        if(empty($cek_pesanan_detail))

        {

            $pesanan_detail = new PesananDetail;

            $pesanan_detail->post_id = $post->id;

            $pesanan_detail->pesanan_id = $pesanan_baru->id;

            $pesanan_detail->jumlah = $request->jumlah_pesan;

            $pesanan_detail->jumlah_harga = $post->harga*$request->jumlah_pesan;

            $pesanan_detail->save();

        }else

        {

        $pesanan_detail = PesananDetail::where('post_id', $post->id)->where('pesanan_id', $pesanan_baru->id)->first();

        $pesanan_detail->jumlah = $pesanan_detail->jumlah+$request->jumlah_pesan;


        // harga sekarang setelah update

        $harga_pesanan_detail_baru = $post->harga*$request->jumlah_pesan;

        $pesanan_detail->jumlah_harga = $pesanan_detail->jumlah_harga+$harga_pesanan_detail_baru;

        $pesanan_detail->update();

        }

        // jumlah total

        $pesanan = Pesanan::where('user_id', Auth::user()->id)->where('status',0)->first();

        $pesanan->jumlah_harga =  $pesanan->jumlah_harga+$post->harga*$request->jumlah_pesan;

        $pesanan->update();


            return redirect('home');

        }


        public function check_out()

        {

            $pesanan = Pesanan::where('user_id', Auth::user()->id)->where('status',0)->first();

        if(!empty($pesanan))

        {

            $pesanan_details = PesananDetail::where('pesanan_id', $pesanan->id)->get();

        }


        return view('checkout.check_out', compact('pesanan','pesanan_details'));

        }


        public function delete($id)

        {

            $pesanan_detail = PesananDetail::where('id', $id)->first();


            $pesanan = Pesanan::where('id', $pesanan_detail->pesanan_id)->first();

            $pesanan->jumlah_harga = $pesanan->jumlah_harga-$pesanan_detail->jumlah_harga;

            $pesanan->update();


            $pesanan_detail->delete();

            // Allert::error('Pesanan Sukses Dihapus', 'Hapus');

            return redirect('check-out');

        }

        public function konfirmasi()

        {

            $pesanan = Pesanan::where('user_id', Auth::user()->id)->where('status',0)->first();

            $pesanan->status = 1;

            $pesanan->update();


            return redirect('check-out');

        }

    }


Views check_out.blade.php:


{{-- @dd($posts) --}}

@extends('layouts.main')

@section('container')

    <div class="container">

        <script src="https://unpkg.com/feather-icons"></script>


        <div class="row justify-content-center">

            <div class="col-md-12">

                <div class="card">

                    <div class="card-header">

                        <h3><i class="me-2" data-feather="shopping-cart"></i>Check Out</h3>

                        @if(!empty($pesanan))

                    </div>

                    <div class="card-body">

                        <table class="table table-bordered">

                            <thead>

                                <tr>

                                    <th>No</th>

                                    <th>Nama Barang</th>

                                    <th>Jumlah</th>

                                    <th>Harga</th>

                                    <th>Total Harga</th>

                                    <th>Aksi</th>

                                </tr>

                            </thead>

                            <tbody>

                                <?php $no = 1; ?>

                                @foreach ($pesanan_details as $pesanan_detail )

                                <tr>

                                    <td>{{ $no++ }}</td>

                                    <td>{{ $pesanan_detail->post->title }}</td>

                                    <td>{{ $pesanan_detail->jumlah }} Style</td>

                                    <td align="left">Rp. {{ number_format($pesanan_detail->post->harga) }}</td>

                                    <td align="left">Rp. {{ $pesanan_detail->jumlah_harga }}</td>

                                    <td>

                                        <form action="{{ url('check-out')}}/{{ $pesanan_detail->id }}" method="post">

                                            @csrf

                                            {{ method_field('DELETE') }}

                                            <button type="submit" class="btn btn-danger btn-sm"><i data-feather="trash-2"></i></button>

                                        </form>

                                    </td>

                                </tr>

                                @endforeach

                                <tr>

                                    <td colspan="4" align="center"><strong>Total Harga :</strong></td>

                                    <td><strong>Rp. {{ number_format($pesanan->jumlah_harga) }}</strong></td>

                                    <td>

                                        <a href="{{ url('konfirmasi-check-out') }}" class="btn btn-success">

                                        <i data-feather="shopping-cart"></i>Check Out

                                        </a>

                                    </td>

                                </tr>

                            </tbody>

                        </table>

                        @endif

                    </div>

                </div>

            </div>

            {{-- <button type="submit" href="/home" class="btn btn-primary mt-3">Kembali</button> --}}

            <a href="/home" class="d-block mt-3 text-decoration-none">Kembali</a>

        </div>

    </div>

    <br>

    <br>

@endsection


Solution

That's probably because $pesanan is empty so the program doesn't read inside if (!empty($pesanan) and $pesanan_details becomes undefined because you don't declare it anywhere except that statement. Please make sure you have data in $pesanan or you need to handle if $pesanan also empty. Maybe in else statement like $pesanan_details = null;


Semoga membantu bang


Answered by: >ioiofadhil

Credit: >StackOverflow


Blog Links

>How to manage the Text in the container in Django?

>Fix webapp stops working issue in Django- Python webapp

>Creating a form in Django to upload a picture from the website

>Sending Audio file from Django to Vue

>How to keep all query parameters intact when changing page in Django?

>Solved: TaskList View in Django

>Implement nested serializers in the Django rest framework

>How to filter events by month in Django?

>Sorting the restframework in Django

>Ways to access instances of models in view in order to save both forms at once in Django

>What makes index.html have such kind of name in Django?

>Fix Module Not Found during Deployment- Django

>Creating a Django with existing directories, files, etc.?

>How to Read a CSV file with PHP using cURL?


Nisha Patel

Nisha Patel

Submit
0 Answers