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 AppHttpControllers;
use AppModelsPesanan;
use AppModelsPost;
use AppModelsPesananDetail;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use AppHttpControllersController;
use AppHttpRequestsStorePesananRequest;
use AppHttpRequestsUpdatePesananRequest;
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
2
Answers
That’s probably because
$pesanan
is empty so the program doesn’t read insideif (!empty($pesanan)
and$pesanan_details
becomesundefined
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 inelse
statement like$pesanan_details = null;
Semoga membantu bang
The problem is in
check_out controller
, before the if you most define your variable as null. it’s because if your if returnsfalse
, the variable defined before and you compact will work okay, but else, the variable is not define and returns to you this error.resolved code: