Skip to content
Advertisement

How to display link delete to auth user in Laravel?

I’m having a simple message/comment features. The message can be deleted by using SoftDeletes function in Laravel. However, I want to make the message can be deleted or the link message being display by the logged-in user only. They cannot delete other’s message. So, here is my code in

message.blade.php

JavaScript

I already tried by using @if (Auth::check()) but there is no difference, the delete link still show up for all users.

JavaScript

How can I make it?

Advertisement

Answer

I assume you have user_id in messages table or any relationship you have created so you need to do like this

JavaScript

or you can create policy for clean code as Laravel recommend to use that
https://laravel.com/docs/8.x/authorization#creating-policies

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement