I do it. Because you shouldn't go to create page is you couldn't save it.
Should policies be checked at create() and store()?
In the documentation I find examples for the action that normally would return the corresponding view. But what about the action that does the actual creating, the store() method. Must both not get the same policy checks?
Perhaps like so:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class PostController extends Controller
{
public function create()
{
$this->authorize('create', 'App\Post');
//
}
public function store(Request $request)
{
$this->authorize('create', 'App\Post');
//
}
}
This look like good practice to me, but I don't find it mentioned in the docs. Some feedback would be appreciated!
@webplaats Yes. Why should you be able to view the create form if you can’t actually persist the model?
If you have a policy and you use $this->authorizeResource(Post::class) in your controller’s constructor, then Laravel will apply the create() policy method on both the create() and store() controller actions. It also means you don’t need to add $this->authorize() calls to every method if you’re using a resource controller.
Please or to participate in this conversation.