Home > database >  Laravel keeps showing 404 as if the directory or file doesn't exist
Laravel keeps showing 404 as if the directory or file doesn't exist

Time:09-09

I am trying to link to an edit page in my admin Laravel admin dashboard.

However for some reason when I click on the link it keeps showing the 404 page as if it didn't find the edit view.
I tried moving the views to others directories, changing the Route, clear the route's cache but it didn't fix the issue.

The link that is supposed to take you to the edit page

<a href="/admin/posts/{{ $post->id }}/edit" >Edit</a>

Routes associated with the admin side of the website

// Admin
Route::post('admin/posts', [AdminPostController::class, 'store'])->middleware('admin');
Route::get('admin/posts/create', [AdminPostController::class, 'create'])->middleware('admin');
Route::get('admin/posts', [AdminPostController::class, 'index'])->middleware('admin');
Route::get('admin/posts/{post}/edit', [AdminPostController::class, 'edit'])->middleware('admin');
Route::patch('admin/posts/{post}', [AdminPostController::class, 'update'])->middleware('admin');
Route::delete('admin/posts/{post}', [AdminPostController::class, 'destroy'])->middleware('admin');

Directories of the project

This is the edit function inside the AdminPostController

 public function edit(Post $post)
        {
            return view('admin.posts.edit', ['post' => $post]);
        }

Post model:


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

//    protected $guarded = [];
//   protected $fillable = ['title','slug'];

    protected $with = ['category', 'author'];

    public function getRouteKeyName()
    {
        return 'slug';
    }

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function author()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function scopeFilter($query, array $filters)
    { 
        $query->when($filters['search'] ?? false, fn($query, $search) =>
            $query->where(fn($query)=>
                $query->where('title', 'like', '%' . $search . '%')
                    ->orWhere('body', 'like', '%' . $search . '%')
            )
        );

        $query->when($filters['category'] ?? false, fn($query, $category) =>
                $query->whereHas('category', fn($query) =>
                        $query->where('slug', $category))

        );

        $query->when($filters['author'] ?? false, fn($query, $author) =>
                $query->whereHas('author', fn($query) =>
                    $query->where('username', $author))

        );
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }



}

CodePudding user response:

The problem is with the getRouteNameKey method which is overriding the default key used by Laravel to identify models in your database and perform route model binding.

Currently, you're telling Laravel to use the slug property of your Post model as the route model identifier, however, your link is passing an ID. So you either need to remove the getRouteNameKey method from your Post model which will return everything to the default ID route model identifier, or provide your link with a Post slug.

<a href="/admin/posts/{{ $post->slug }}/edit" >
    Edit
</a>

Thereafter Laravel should be able to successfully perform route model binding.

CodePudding user response:

try :

Route::get('admin/posts', [AdminPostController::class, 'index'])->middleware('admin');
Route::get('admin/posts/create', [AdminPostController::class, 'create'])->middleware('admin');
Route::post('admin/posts', [AdminPostController::class, 'store'])->middleware('admin');
Route::get('admin/posts/{post}/edit', [AdminPostController::class, 'edit'])->middleware('admin');
Route::patch('admin/posts/{post}', [AdminPostController::class, 'update'])->middleware('admin');
Route::delete('admin/posts/{post}', [AdminPostController::class, 'destroy'])->middleware('admin');
  • Related