6

In Laravel Nova I have BelongsToMany Relationship (companies - pivot - requests).

In the Pivot table I have some additional columns which I access with the pivot fields (https://nova.laravel.com/docs/1.0/resources/relationships.html#belongstomany) which works great.

But now I have a special case where I have an additional BelongsTo relationship from the pivot table to a third table (states). I tried to define a BelongsTo Field in the pivot Fields, but that is not working.

BelongsToMany::make('Companies', 'companies', Company::class)->fields(new CompanyRequestFields()),

pivot fields:

class CompanyRequestFields
{
    /**
     * Get the pivot fields for the relationship.
     *
     * @return array
     */
    public function __invoke()
    {
        return [
            Number::make('preis'),
            Text::make('bezahlt'),
             BelongsTo::make('State', 'state', States::class),
        ];
    }
}

The error I get:

Call to undefined method Illuminate\Database\Eloquent\Relations\Pivot::state()

The relationship state() actually exists on the pivot Model and there is a Nova resource State class too.

So it looks like this is not supported from PivotFields? Or does anyone know if its possible to accomplish this?

Saumini Navaratnam
  • 8,439
  • 3
  • 42
  • 70
ndberg
  • 3,391
  • 1
  • 21
  • 36

2 Answers2

5

I was not able to make it work with relationship in pivot table, but you can achieve it without a relationship definition like below.

class CompanyRequestFields
{
    public function __invoke()
    {
        $states= \App\State::all()->pluck('name', 'id');

        return [
            ...
            Select::make('State')->options($states),
        ];
    }
}

In \App\Request model

public function companies()
{
    return $this->belongsToMany('App\Company')->withPivot('state');
}

Hope this approach will help you.

Saumini Navaratnam
  • 8,439
  • 3
  • 42
  • 70
-5

This is for Normal Laravel, Just change the sintaxis to laravel nova if you can!

In Normal Laravel, If you would like to define a custom model to represent the intermediate table of your relationship, you may call the using method when defining the relationship. Custom many-to-many pivot models should extend the Illuminate\Database\Eloquent\Relations\Pivot class while custom polymorphic many-to-many pivot models should extend the Illuminate\Database\Eloquent\Relations\MorphPivot class. In your case, you may define a custom Companies_requests pivot model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;

class Companies_requests extends Pivot
{
    //
}

And reference it in your company model and request model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Company extends Model
{
    public function requests()
    {
        return $this->belongsToMany('App\Request')->using('App\Companies_requests');
    }
}

.

namespace App;

use Illuminate\Database\Eloquent\Model;

class Request extends Model
{
    public function companies()
    {
        return $this->belongsToMany('App\Company')->using('App\Companies_requests');
    }
}

Now, the ->pivot will be your new model!

Hope this is what you're searching for:)