Home > Net >  ErrorException Trying to get property 'slug' of non-object
ErrorException Trying to get property 'slug' of non-object

Time:12-10

I am a beginner to Laravel 9. I am getting error when I click edit form, and after click edit form i'm not able to submit edit but click dashboard menu in sidebar, after that given error Trying to get property 'slug' of non-object, i look at URL, URL changed to http://127.0.0.1:8000/laporan-edit/dashboard, should be there link is http://127.0.0.1:8000/dashboard.

My Controller

<?php

namespace App\Http\Controllers;

use App\Models\Laporan;
use Illuminate\Http\Request;

class LaporanController extends Controller
{
    public function index()
    {
        $laporan = Laporan::all();
        return view('laporan', ['laporan' => $laporan]);
    }

    public function add()
    {
        return view('laporan-add');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'nama_korban' => 'required|max:255'
        ]);

        if ($request->file('foto')) {
            $extension = $request->file('foto')->getClientOriginalExtension();
            $NamaBaru = $request->nik_korban . '-' . now()->timestamp . '.' . $extension;
            $request->file('foto')->storeAs('fotokorban', $NamaBaru);
        }

        $request['foto_korban'] = $NamaBaru;
        Laporan::create($request->all());
        return redirect('laporan')->with('status', 'Data Telah Ditambahkan !');
    }

    public function edit($slug)
    {
        $laporan = Laporan::where('slug', $slug)->first();
        return view('laporan-edit', ['laporan' => $laporan]);
    }


    public function update(Request $request, $slug)
    {

        $laporan = Laporan::where('slug', $slug)->first();
        $laporan->update($request->all());
        return redirect('laporan')->with('status', 'Data Telah Di Edit');
    }
}

My Model File

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Laporan extends Model
{
    use HasFactory;
    use Sluggable;

    protected $fillable = [
        'petugas_pendamping', 'kronologi_kejadian', 'id_pengguna', 'nama_pelaku', 'jenis_kasus', 'foto_korban', 'tempat_kejadian', 'alamat_kejadian', 'wilayah_pendamping', 'korban', 'slug', 'nama_pelapor', 'nik_pelapor', 'alamat_pelapor', 'hubungan_pelapor', 'nik_korban', 'nama_korban', 'jk_korban', 'umur_korban', 'alamat_korban', 'nik_pelaku', 'jk_pelaku', 'umur_pelaku', 'alamat_pelaku', 'tanggal_pengaduan', 'tanggal_kejadian', 'jam_pengaduan', 'status_laporan'
    ];

    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'nama_pelapor'
            ]
        ];
    }
}

My Route

<?php

use Illuminate\Auth\Events\Login;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\RekapController;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\PetugasController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\DashboardController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
})->middleware('auth');

Route::middleware('only_guest')->group(function () {
    Route::get('login', [AuthController::class, 'login'])->name('login');
    Route::post('login', [AuthController::class, 'authenticating']);
});

Route::middleware('auth')->group(function () {
    Route::get('logout', [AuthController::class, 'logout']);
    Route::get('dashboard', [DashboardController::class, 'index'])->middleware('only_admin');
    Route::get('petugas', [PetugasController::class, 'petugas'])->middleware('only_petugas');
    Route::get('rekaplaporan', [RekapController::class, 'index']);

    Route::get('laporan', [LaporanController::class, 'index']);
    Route::get('laporan-add', [LaporanController::class, 'add']);
    Route::post('laporan-add', [LaporanController::class, 'store']);
    Route::get('laporan-edit/{slug}', [LaporanController::class, 'edit']);
    Route::put('laporan-edit/{slug}', [LaporanController::class, 'update']);


    Route::get('user', [UserController::class, 'index']);
    Route::get('profile', [ProfileController::class, 'index']);
});

View edit.blade.php

<div >
                        <form action="/laporan-edit/{{ $laporan->slug }}" method="post" enctype="multipart/form-data" >
                            @csrf
                            @method('put')

Im Already Try with condition like this

public function edit($slug)
    {
        if($laporan = Laporan::where('slug', $slug)->first()){
            return view('laporan-edit', ['laporan' => $laporan]);
        } else{
            return redirect('dashboard');
        }
        // $laporan = Laporan::where('slug', $slug)->first();
        // return view('laporan-edit', ['laporan' => $laporan]);
    }

it's working but, when i click sidebar another name, then it always redirects http://127.0.0.1:8000/dashboard

CodePudding user response:

In the blade if the side menu, use absolute link href="/dashboard" instead of relative link href="dashboard".

<ul  id="sidebar-menu">
    <li >
        <a  href="#" tabindex="-1">
            <span >Home</span>
            <span  data-bs-toggle="tooltip" title="Home" data-bs-placement="right">-</span>
        </a>
    </li>
    <li >
        <a @if(request()->route()->uri == 'dashboard')  
        @else  @endif
        aria-current="page" href="/dashboard">

or use named routes by giving an alias/name to the route and use it to generate the link

Route::get('dashboard', [DashboardController::class, 'index'])->middleware('only_admin')->name('dashboard');

in blade

<a @if(request()->route()->uri == 'dashboard')  
        @else  @endif
        aria-current="page" href="{{route('dashboard')}}">

You should also use $laporan = Laporan::where('slug', $slug)->firstOrFail(); instead of ->first() to avoid unnecessary errors and show 404 when the link is incorrect.

  • Related