php - Laravel: 403 This action is unauthorized
Get the solution ↓↓↓I'm having a problem with authorizing a specific user to only click the button but when i tried to press the button is always say Unathorized even though i already dump and die the authorization which is say istrue
don't know which part of the code is wrong. Here is the code.
Conversation Policy
public function update(User $user, Conversation $conversation)
{
//
return $conversation->user->is($user);
}
Conversation Controller
public function store(Reply $reply)
{
$this->authorize('update', $reply->conversation);
$this->conversation->bestreply($reply);
}
The HTML
@foreach ($conversation->replies as $reply)
<hr>
<div>
<p>{{ $reply->user->name }} said...</p>
{{ $reply->comment }}
@can('update',$conversation)
<form action="/best-reply/{{ $reply->id }}" method="post">
@csrf
<button type="submit">best reply</button>
</form>
@endcan
</div>
@continue($loop->last)
@endforeach
Conversation Model
public function bestreply(Reply $reply)
{
return $this->best_reply = $reply->id;
$this->save();
}
public function user()
{
return $this->belongsTo(User::class);
}
Reply Model
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
Web Route
Auth::routes();
Route::get('/conversations', 'ConversationsController@index')->middleware('auth');
Route::get('/conversations/{conversation}', 'ConversationsController@show');
Route::post('/best-reply/{reply}', 'ConversationBestReplyController@store');
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/payment/create', 'PaymentController@index')->middleware('auth');
Route::post('/payment', 'PaymentController@store')->middleware('auth');
Route::get('/notifications', 'UserNotificationsController@show')->middleware('auth');
Middle ware
public function middleware($middleware = null)
{
if (is_null($middleware)) {
return (array) ($this->action['middleware'] ?? []);
}
if (is_string($middleware)) {
$middleware = func_get_args();
}
$this->action['middleware'] = array_merge(
(array) ($this->action['middleware'] ?? []), $middleware
);
return $this;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: your lock file does not contain a compatible set of packages. please run composer update.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.