z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.13k stars 2.81k forks source link

How to display pivot table's field ? #5727

Closed r-shaygan closed 1 year ago

r-shaygan commented 1 year ago

there is a many-to-many relationship between coverage and proposal model.

on the proposal model , i have :

public function coverages()
    {
        return $this->belongsToMany(InsuranceCoverage::class,'health_coverage_proposal',
            'proposal_id','coverage_id')->withPivot('cost');
    }

i want to access 'cost' column on ProposalController:

protected function detail($id)
    {
        $show = new Show(HealthProposal::findOrFail($id));

        $show->coverages(__('Insurance coverages'), function ($coverages) {
            $coverages->resource('/'.config('admin.route.prefix').'/insurances/coverages');
            $coverages->id();
            $coverages->title(__('Insurance coverages title'));
            $coverages->cost();// it doesnt work

        });
}
alexoleynik0 commented 1 year ago

Have you tried something like $coverages->column('pivot.cost'); ?

r-shaygan commented 1 year ago

Have you tried something like $coverages->column('pivot.cost'); ?

it returns this error: Call to undefined relationship [pivot] on model [Modules\Insurance\Entities\InsuranceCoverage]

alexoleynik0 commented 1 year ago

Hmm.. can't test right now. Does your model's relationship has ->withPivot ('cost') ?

On Fri, Mar 3, 2023, 09:23 r-shaygan @.***> wrote:

Have you tried something like $coverages->column('pivot.cost'); ?

it returns this error: Call to undefined relationship [pivot] on model [Modules\Insurance\Entities\InsuranceCoverage]

— Reply to this email directly, view it on GitHub https://github.com/z-song/laravel-admin/issues/5727#issuecomment-1453090328, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALJJJEISUB22P2NMRJPHIYLW2GL6BANCNFSM6AAAAAAVN6I4OE . You are receiving this because you commented.Message ID: @.***>

r-shaygan commented 1 year ago

Hmm.. can't test right now. Does your model's relationship has ->withPivot ('cost') ?

yes..

public function coverages()
    {
        return $this->belongsToMany(InsuranceCoverage::class,'health_coverage_proposal',
            'proposal_id','coverage_id')->withPivot('cost');
    }

and i don't know how to insert this pivot field either

alexoleynik0 commented 1 year ago

You're right, sorry, confused relation columns with nested ones. Grid treats column names as relations if there's a dot between names like in pivot.cost, but pivot is not a relation - hence an error. You should treat it like JSON field / array / object on the Grid's model. $coverages->column('pivot->cost'); -- your way to go.

r-shaygan commented 1 year ago

You're right, sorry, confused relation columns with nested ones. Grid treats column names as relations if there's a dot between names like in pivot.cost, but pivot is not a relation - hence an error. You should treat it like JSON field / array / object on the Grid's model. $coverages->column('pivot->cost'); -- your way to go.

it worked.. thank you.. can you help me, with how to get pivot value from a form?

$form->multipleSelect('roles','Role')->options(Role::all()->pluck('name','id'));

in the code above we can just get the related model value.. what if it has pivot field?