php – Laravel 5: Trying to get property 'file_gambar_produk' of non-object-ThrowExceptions

Exception or error:

I have a problem with my code, at first everything was fine, but after a few weeks I left my laptop and I tried to run my project again, suddenly this error occurred

Error Handler :

Trying to get property ‘file_gambar_produk’ of non-object (View:
C:\xampp\htdocs\PROJECT-CAOS-TEST2\Project Modules\Project(Modul7) –
Copy\resources\views\admin\pesanan\index.blade.php)

Error in line:

@foreach($data as $row)
<div class="row mb-3">
    <div class="col-6">
        <i class="fas fa-user"></i> {{ $row->nama_pelanggan }}
    </div>
    <div class="col-6 text-right text-muted">
        <small>
            {{ date('d M Y H:i:s',strtotime($row->tanggal_pesan)) }} /
            {{ date('d M Y H:i:s',strtotime($row->tanggal_update)) }}           
        </small>
    </div>
</div>
<div class="row">
    <div class="col-4">
        <img src="{{ url('images/'.$row->produk->file_gambar_produk) }}" width="100" class="img-thumbnail mr-1" align="left">
        <strong>{{ $row->produk->nama_produk }}</strong> <br>
        Kategori : {{ $row->produk->kategori->nama_kategori }} <br>
        Jumlah : {{ $row->jumlah }}
    </div>
    <div class="col-3 text-success">
        <strong>Rp. {{ number_format($row->total_harga,0,',','.') }}</strong>
    </div>
    <div class="col-3">
        {{ ucwords(str_replace('_',' ',$row->status_pesanan)) }}
    </div>
    <div class="col-2 text-right">
        <a href="{{ route('pesanan.edit',['pesanan'=>$row->id]) }}" class="btn btn-warning">Lihat rincian</a>
    </div>
</div>
<hr>
@endforeach

My Routes :

    Route::group(['prefix' => 'admin','middleware' => ['auth']], function() {
    Route::get('/','DashboardController@index')->name('dashboard');
    Route::resource('kategori', 'KategoriController');
    Route::resource('produk', 'ProdukController');
    Route::resource('pesanan', 'PesananController');
    Route::get('user/profil','UserController@profil')->name('user.profil');
    Route::post('user/profil','UserController@updateProfil');
    Route::get('user/profil','UserController@profil')->name('user.profil');
    Route::post('user/profil','UserController@profilUpdate')->name('user.profil.update');
    Route::resource('user', 'UserController');
});

My Controller :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Pesanan;

class PesananController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $status = $request->status;
        $sortir = $status == 'lunas' || $status == "dikirim" ? 'asc' : 'desc';
        $result = Pesanan::where('status_pesanan','like',"%{$status}%")
                ->orderBy('tanggal_update', $sortir)->paginate(10);
        return view('admin.pesanan.index',['data'=>$result]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Pesanan $pesanan)
    {
        return view('admin.pesanan.edit',['row'=>$pesanan]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Pesanan $pesanan)
    {
        $request->validate([
            'status'=>'required|in:belum_bayar,lunas,dikirim,selesai,batal'
        ]);

        $pesanan->update([
            'status_pesanan'=>$request->status,
        ]);

        return redirect()->route('pesanan.index')->with('update','Berhasil diupdate!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *