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" class="text-blue-500 hover:text-blue-600">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');
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);
}
}
