ポリシーを作成してください:php artisan make:policy
。
登録ポリシー:ポリシーのロジックで
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
Post::class => PostPolicy::class,
];
、例えば:
public function create(User $user, Post $post)
{
$groupsOfUser = $user->groups()->get();
foreach ($groupsOfUser as $group) {
if($group->id == request('groupId'))return true;
}
}
そして、あなたのコントローラーで:
public function store(Post $post, $groupId)
{
$this->authorize('create', $post);
Post::create([
'user_id' =>auth()->id(),
'group_id' => $groupId,
'title' => 'sometitle',
]);
}
と私はルートでそれをテストしています:
Route::get('/post/{groupId}', '[email protected]');
しかし、あなたは入力を介してgroupIdを取得している可能性がありますが、あなたはそのアイデアを得ます。
そのコードが実際に広すぎる、具体的な問題を共有しています。 – C2486
これについての情報を調べようとしています。どこでも私はこの質問を更新します。 –