Php Can';使用Laravel 5.7中的软删除功能无法删除详细信息
我在laravel中使用了软删除功能,当我使用Php Can';使用Laravel 5.7中的软删除功能无法删除详细信息,php,laravel-5,Php,Laravel 5,我在laravel中使用了软删除功能,当我使用Post::withTrashed()->get()方法加载所有数据时,它是正常的。但是,当我想使用此查询获取数据的详细信息时,它会将我转到404页。请帮忙 我也尝试了Post::onlyTrashed()->find($Post)->get(),但还是一样 我通过直接回显一个helloworld字符串来检查路由文件,并且工作正常 更新 控制器脚本 public function fetchDeletedPosts() { return Po
Post::withTrashed()->get()方法加载所有数据时,它是正常的。但是,当我想使用此查询获取数据的详细信息时,它会将我转到404页。请帮忙
我也尝试了Post::onlyTrashed()->find($Post)->get()
,但还是一样
我通过直接回显一个helloworld字符串来检查路由文件,并且工作正常
更新
控制器脚本
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脚本
Route::get('posts/deleted', 'PostController@fetchDeletedPosts')->name('posts.removed');
Route::get('posts/deleted/{post}/edit', 'PostController@edit');
<?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);
}
}
Post.php脚本
Route::get('posts/deleted', 'PostController@fetchDeletedPosts')->name('posts.removed');
Route::get('posts/deleted/{post}/edit', 'PostController@edit');
<?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);
}
}
Laravel依赖项注入容器将已经为您获取数据。但是你有一个被删除的帖子。这就是你得到404的原因。
因此,请将路线改为:
Route::get('posts/deleted/{id}/edit', 'PostController@edit');
而你的控制器
public function edit($id)
{
$posts = Post::withTrashed()->find($id);
return view('post.edit', compact('posts'));
}
404表示找不到页面。检查您的路线。是否尝试了->where()
?恐怕withTrashed
返回的是一个生成器,而不是find
工作的模型。。cmiiw@FelippeDuarte,我在问题中确实解释了这一点。路线很好,我可以抓到未删除的data@BagusTesa,其中函数也不允许我访问软删除的数据,它也将我发送到404。@Fellipe,它起作用了,谢谢。但是当我使用slug
而不是id
时,它仍然给了我404。你可以通过slug进行查询:Post::withTrashed()->where('slug',$slug)->first()
。只需将slug
的id
更换为路由和功能中的parameter@Fellipe,成功了!谢谢有一天我们去starb*cks吧,哈哈