garrettmassey's avatar

Looking for a package that allows me to "add" models through a HasMany relationship in Nova

Nova allows us to attach items on a given resource through the BelongsToMany field, which is nice, but there are instances where I do not have a BelongsToMany relationship, and instead have a HasMany relationship defined, and I would like to be able to "add" items to the resource through that relationship without creating a new one.

For example: Let's say I have a model/resource called "Department". A User belongsTo a Department and any given Department hasMany Users. In Laravel currently, there is no way to add an existing user to an existing department from the Department resource. Instead, I have to go to the User's specific resource detail page and then add the Department from there.

I would love to find a package that allows me to go to a Department's detail or edit page and then add / update existing Users' department assignment from that department. So if John belongs to the "Finance" department, and I want to move him to the "HR" department, I would go to the HR resource page, and then add "John" as a staff member. The details on the HR resource wouldn't change, but instead John's 'department_id' would change from the "Finance" id to the "HR" id.

Does anyone know of a package that does this? I found blendbyte/nova-attach-many but it does not seem to easily support the HasMany relationship, and instead focuses on the BelongsToMany side.

0 likes
1 reply
LaryAI's avatar
Level 58

To achieve the functionality you're looking for in Laravel Nova, you can use a combination of custom fields and actions to manage HasMany relationships in a way that allows you to "add" existing models to a resource. While there might not be a package that directly supports this out-of-the-box for HasMany relationships, you can implement a solution using Nova's existing features.

Here's a step-by-step approach to achieve this:

  1. Create a Custom Action:

    You can create a custom Nova action that allows you to update the department_id of a User directly from the Department resource page.

    First, generate a new action:

    php artisan nova:action AssignUserToDepartment
    

    Then, implement the action logic in AssignUserToDepartment.php:

  2. Attach the Action to the Department Resource:

    In your Department Nova resource, attach the action:

    namespace App\Nova;
    
    use App\Nova\Actions\AssignUserToDepartment;
    use Laravel\Nova\Resource;
    use Laravel\Nova\Fields\ID;
    use Laravel\Nova\Fields\Text;
    use Laravel\Nova\Fields\HasMany;
    
    class Department extends Resource
    {
        // ...
    
        public function fields(Request $request)
        {
            return [
                ID::make()->sortable(),
                Text::make('Name'),
                HasMany::make('Users'),
            ];
        }
    
        public function actions(Request $request)
        {
            return [
                new AssignUserToDepartment,
            ];
        }
    }
    
  3. Usage:

    • Navigate to the Department resource in Nova.
    • Use the AssignUserToDepartment action to assign a user to the department by entering the user's ID.

This approach allows you to manage HasMany relationships by updating the foreign key directly from the parent resource, effectively "adding" existing models to the relationship. You can further customize the action to include a dropdown of users or other UI enhancements as needed.

Please or to participate in this conversation.