laravel / nova-issues

554 stars 34 forks source link

BelongsToMany Field Not Working on Create/Edit Forms #6536

Closed badrshs closed 1 week ago

badrshs commented 1 week ago

Environment

Issue Description

I'm developing an education course website using Laravel Nova. I'm trying to implement a feature in my CourseSession resource that allows an admin to assign multiple beneficiaries (students) to a session at once. However, the current implementation is not working as expected on any view.

Current Behavior

Expected Behavior

On views (create, and edit):

  1. There should be a field where the admin can select multiple beneficiaries at once.
  2. When the form is submitted, it should create multiple entries in the beneficiary_session pivot table, one for each selected beneficiary.
  3. The interface should allow for easy selection of multiple beneficiaries without having to add them one by one.

Current Database Structure

Current Implementation

Here's my current implementation in the CourseSession Nova resource:

<?php

namespace App\Nova;

use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\BelongsToMany;
use Laravel\Nova\Http\Requests\NovaRequest;

class CourseSession extends Resource
{
    public static $model = \App\Models\CourseSession::class;

    public function fields(NovaRequest $request)
    {
        return [
            ID::make()->sortable(),

            BelongsTo::make('Course'),

            // Other fields for CourseSession...

            BelongsToMany::make('Beneficiaries')
                ->searchable() // I don't care at the moment about the filter, I just need  the BelongsToMany to work
        ];
    }
}

And here are the relevant parts of my models:

// App\Models\CourseSession.php
class CourseSession extends Model
{
    public function course()
    {
        return $this->belongsTo(Course::class);
    }

    public function beneficiaries()
    {
        return $this->belongsToMany(Beneficiary::class, 'beneficiary_session'));
    }
}

// App\Models\Beneficiary.php
class Beneficiary extends Model
{
    public function courses()
    {
        return $this->belongsToMany(Course::class);
    }

    public function sessions()
    {
        return $this->belongsToMany(CourseSession::class, 'beneficiary_session');
    }
}

// App\Models\Course.php
class Course extends Model
{
    public function sessions()
    {
        return $this->hasMany(CourseSession::class);
    }

    public function beneficiaries()
    {
        return $this->belongsToMany(Beneficiary::class);
    }
}

The Problem

The multi-select functionality for beneficiaries is not working as expected on any view:

  1. On the create form, I'm unable to select and assign multiple beneficiaries to a new CourseSession.
  2. On the edit form, I can't modify multiple assigned beneficiaries or add multiple new ones to an existing CourseSession.

Questions

  1. How can I modify my BelongsToMany field to allow multiple selections on all views (create, and edit)?
  2. Is there a specific Nova field or configuration I should be using to enable bulk assignment of beneficiaries instead of this?
  3. Are there any known issues or limitations with BelongsToMany fields in Nova that could be preventing multi-select functionality?

Any help or guidance would be greatly appreciated. Thank you!

jeremynikolic commented 1 week ago

Thanks for reaching out @badrshs 🙏

A couple of misunderstanding here:

For further assistance setting things up, please reach out to our support via the Nova website.

As this repo is dedicated for discussing bugs or feature request/improments.