Laravel 5: allow user to edit post if he's the owner of the post or the owner of the forum category
So how can I check if the users is the owner of the article OR the owner of the category to display and allow EDIT?
Use Laravel’s new authorization component.
EDIT: I think you’re misunderstanding how authorization should be used. It should be used to check if the current user can perform an action (or not). Therefore, you don’t want to be defining multiple methods for different users types.
Take editing a post for example. There’s the name of your authorization check: @can('edit', $post)
. You don’t need to define a different one for regular users, and another for moderators. Just add the logic to your edit post method:
class PostPolicy
{
public function edit(User $user, Post $post)
{
// If user is administrator, then can edit any post
if ($user->isModerator()) {
return true;
}
// Check if user is the post author
if ($user->id === $post->author_id) {
return true;
}
return false;
}
}
As you can see, I’m doing the different checks in the same method, so in your Blade template you can just do this:
@can('edit', $post)
<a href="{{ route('post.edit', $post->getRouteKey()) }}">Edit post</a>
@endcan
Hope this clears things up.