php – Laravel Nova – Hide update icon (edit attached) on related resource-ThrowExceptions

Exception or error:

Does anyone know how I get rid of the update icon on a related resource’s details page?

I’m talking about the icons on the right here;

Nova Backend

Events and Time Slots have a many to many relationship.

They are related like so;

App/TimeSlot.php

public function events()
{
    return $this->belongsToMany('App\Event');
}

App/Event.php

public function timeSlots()
{
    return $this->belongsToMany('App\TimeSlot');
}

I have Nova resources and policies for each of those models too.

In my Time Slot policy, update is set to return false.

App/Nova/Policies/TimeSlotPolicy.php

/**
 * Determine whether the user can update the time slot.
 *
 * @param  \App\User  $user
 * @param  \App\TimeSlot  $timeSlot
 * @return mixed
 */
public function update(User $user, TimeSlot $timeSlot)
{
    return false;
}

and in my Event policy attachAnyTimeSlot and detachTimeSlot are both set to return false;

App/Nova/Policies/EventPolicy.php

/**
 * Determine whether the user can attach a time slot to an event.
 *
 * @param  \App\User  $user
 * @param  \App\Event $event
 * @return mixed
 */
public function attachAnyTimeSlot(User $user, Event $event)
{
   return false;
}

/**
 * Determine whether the user can detach a time slot from an event.
 *
 * @param  \App\User  $user
 * @param  \App\Event $event
 * @param  \App\TimeSlot $timeSlot
 * @return mixed
*/
public function detachTimeSlot(User $user, Event $event, TimeSlot $timeSlot)
{
    return false;
}

The latter gets rid of the trash can icon that detaches the associated resource.

Nothing seems to get rid of the edit icon.

When I click it I can’t do anything.

Nova Backend 2

For obvious reasons, I don’t want it to appear at all.

Does anyone know how to remove it?

How to solve:

In your EventPolicy define

public function attachTimeSlot(User $user, Event $event, TimeSlot $timeSlot)
{
   return ! $event->timeslots->contains($timeslot);
}

Taken from this github issue, take a look for more detail

Leave a Reply

Your email address will not be published. Required fields are marked *