【发布时间】:2021-09-08 16:34:51
【问题描述】:
我的 postpolicy 中有一个方法,只有当前经过身份验证的用户才能删除他自己的帖子。我尝试将此方法放在我的删除方法中,但它返回该方法不存在。我在控制器中包含了 authroizerequest,所以我很困惑为什么会出现这个错误。
方法 App\Http\Livewire\Posts::authorize 不存在。
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
public $postId = null;
public function deletePost($id)
{
$this->authorize('delete', $this->postId);
$post = Post::find($id);
Storage::delete('public/photos/', $post->image);
$post->delete();
session()->flash('flash.banner', 'Post Deleted Successfully');
}
政策:
namespace App\Policies;
use App\Models\Post;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
public function delete(User $user, Post $post){
return $user->id === $post->user_id;
}
【问题讨论】: