I have a problem, I cannot use policies in laravel 5.2.
I have 2 tables, students and tasks .
I try to apply a policy to prevent the task from being edited by changing the URL, but I always get the message This action is unauthorized , although the task is the correct user.
Policy Code:
<?php
namespace App\Policies;
use App\Models\Student;
use App\Models\Task;
class TasksPolicy
{
public function edit(Student $student, Task $tasks)
{
return $student->id === $tasks->student_id;
}
}
Code in AuthServiceProvider.php
<?php
namespace App\Providers;
use App\Models\Task;
use App\Policies\TasksPolicy;
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
Task::class => TasksPolicy::class
];
And then the call in the TaskController.php file:
public function edit($id)
{
$tasks = Task::findOrFail($id);
$this->authorize('edit', $tasks);
return view('tasks.edit', compact('tasks'));
}
I think the code is good because I reviewed it several times, but as I said earlier, I always get the message This action is unauthorized , although the task is to edit the user.
http://i.imgur.com/2q6WFb3.jpg
? ?