Reputation: 103
I'm using soft delete feature in laravel, it is ok when I load all data with Post::withTrashed()->get()
method. But when I want to get the detailed information of the data using this query Post::withTrashed()->find($post)->get()
, it throws me to 404 page. Please help.
I tried Post::onlyTrashed()->find($post)->get()
too, but still the same.
I checked the routes file by directly echoing a Hello world string on it and does work normally.
UPDATE
Controller script.
public function fetchDeletedPosts()
{
return Post::onlyTrashed()->paginate(10);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
$posts = Post::withTrashed()->find($post)->first();
return view('post.edit', compact('posts'));
}
web.php script
Route::get('posts/deleted', 'PostController@fetchDeletedPosts')->name('posts.removed');
Route::get('posts/deleted/{post}/edit', 'PostController@edit');
Post.php script
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
protected $fillable = [
'category_id', 'status', 'slug', 'title', 'content-preview', 'content'
];
public function getRouteKeyName() {
return 'slug';
}
public function author() {
return $this->belongsTo(User::class, 'user_id', 'id');
}
public function tags() {
return $this->belongsToMany(Tag::class);
}
}
Upvotes: 2
Views: 2145
Reputation: 15131
Laravel dependency injection container will already fetch the data for you. But you have a deleted post. That's why you got a 404. So change your route to:
Route::get('posts/deleted/{id}/edit', 'PostController@edit');
And your controller to
public function edit($id)
{
$posts = Post::withTrashed()->find($id);
return view('post.edit', compact('posts'));
}
Upvotes: 4