При использовании
$this->authorize('index', Post::class)
возвращается страница 403, а при
Post::find(1)->can('index', Post::class)
возвращается ошибка
Call to undefined method App\Models\Post::can()
web.php:
Route::get('/', [UserController::class, 'index']);
UserConroller.php:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Post;
class UserController extends Controller
{
public function index(Request $request)
{
$this->authorize('index', Post::class);
}
}
PostPolicy.php:
namespace App\Policies;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class PostPolicy
{
use HandlesAuthorization;
public function index()
{
return true;
}
}
AuthServiceProvider.php:
protected $policies = [
Post::class => PostPolicy::class,
];