laravel / nova-issues

553 stars 34 forks source link

BelongsToMany error with User - Laravel\Nova\Resource::resolveFields() must be an instance of Laravel\Nova\Http\Requests\NovaRequest #587

Closed RicLeP closed 6 years ago

RicLeP commented 6 years ago

I have a many to many relationship between Users and Firms.

The User model and resource are standard except the relationship, the Firm is just a model with the relationship and the Resource only has ID, name and the relationship.

When viewing a User resource is it correctly shows the firms however when viewing a Firm it can’t show the Users and has this error:

Argument 1 passed to Laravel\Nova\Resource::resolveFields() must be an instance of Laravel\Nova\Http\Requests\NovaRequest, instance of Illuminate\Http\Request given, called in \nova\src\Resource.php on line 350

I think it might be the same error as here #88

warren32 commented 6 years ago

Can you post the code of your Firm resource from Nova? I think I had this issue today and solved it by making sure the $model property was set properly.

RicLeP commented 6 years ago

Hi @warren32

Here’s the file:

namespace App\Nova;

use Laravel\Nova\Fields\BelongsToMany;
use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;

class Firm extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = 'App\Firm';

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'name';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id', 'name',
    ];

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            Text::make('Name')
                ->sortable()
                ->rules('required', 'max:255'),

            BelongsToMany::make('Users')
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function lenses(Request $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}
RicLeP commented 6 years ago

It seems to be a problem with the User model. If I create a new model for the user table but just extending Model - so not using any of the special auth stuff etc. then the relationship works.

UserRelationship.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserRelationship extends Model
{
    protected $table = 'users';

    public function firms() {
        return $this->belongsToMany(Firm::class, 'firm_user', 'user_id', 'firm_id');
    }
}

Firm.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Firm extends Model
{
    public function users() {
        return $this->belongsToMany(UserRelationship::class, 'firm_user', 'firm_id', 'user_id');
    }
}

EDIT: Okay, the above works when there are no relationships. A soon as one is added the original error it shown.

AegirLeet commented 6 years ago

Does explicitly setting the attribute and resource help? BelongsToMany::make('Users', 'users', User::class)

RicLeP commented 6 years ago

No, sadly not. I still get the error but only when there is an existing relationship in the database. The inverse relationship works fine.

RicLeP commented 6 years ago

Okay, I found the error. In my User Nova resource I was accidentality passing the $request into one of the fields. So when Nova was trying to list the attached resources it errored.