Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

426
Views
Why Laravel Policy is not working in View/Blade?

I tried to filter who can edit/delete on my app using policies but it's not working. Trying to use it on blade.

QuestionPolicy.php

    const UPDATE = 'update';
    const DELETE = 'delete';

    /**
     * Check if user can update a question.
     */
    public function update(User $user, Question $question): bool
    {
        return $question->isAskedBy($user) || $user->isModerator() || $user->isAdmin();
    }

    /**
     * Check if user can delete a question.
     */
    public function delete(User $user, Question $question): bool
    {
        return ($question->isAskedBy($user) || $user->isModerator() || $user->isAdmin()) && !$user->isBanned();
    }

question.blade.php

@can(App\Policies\QuestionPolicy::UPDATE, App\Models\User::class, App\Models\Question::class)
   <a class="text-sm font-light text-gray-600" href="#">Edit</a>
@endcan
@can(App\Policies\QuestionPolicy::DELETE, App\Models\User::class, App\Models\Question::class)
   <a class="text-sm font-light text-gray-600" href="#">Delete</a>
@endcan

Did I do it wrong? Tried to login as admin and also the user who created the question but the link to edit/delete is not rendered.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to pass the actual instance of the question (and you don't need to pass the user class) if the policy is regarding a specific question:

@can(App\Policies\QuestionPolicy::UPDATE, $question)
   <a class="text-sm font-light text-gray-600" href="#">Edit</a>
@endcan
@can(App\Policies\QuestionPolicy::DELETE, $question)
   <a class="text-sm font-light text-gray-600" href="#">Delete</a>
@endcan

$user in the policy is always the currently signed in user.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!