Laravel Custom Pivot Model is missing fields

Sloothword

I am trying to use a custom Pivot Model like:

class A extends Model{
    public function b()
    {
        return $this->belongsToMany(B::class)
            ->using(PivotAB::class);
    }

class PivotAB extends Pivot{}

When accessing PivotAB through the relationship the additional field from the pivot table is missing (output from artisan tinker):

>>>$q = A::all();
=> Illuminate\Database\Eloquent\Collection {#1385
     all: [
       App\Models\A {#1386
         id: 1             
       },
     ],
   }
>>> $q[0]->b[0]->pivot;
=> App\Models\PivotAB {#1389
     a_id: 1,
     b_id: 1,
   }
>>> $q[0]->b[0]->pivot->custom_field;
=> null

But when i query the pivot model directly the field gets populated:

>>> PivotAB::all();    
=> Illuminate\Database\Eloquent\Collection {#1382
     all: [
       App\Models\PivotAB{#281
         a_id: 1,
         b_id: 1,
         custom_field: "abc",
       },
     ],
   }

What am i missing? Do i need to declare the pivot fields somewhere?

Sloothword

I had to add all fields to the relationship with ->withPivot('custom_field'), so they get populated when querying via the relationship on A.

Somehow i understood the Laravel Docs as either having to use ->withPivot(...) or ->using(...), but actually you need to include both.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel: How do I create a custom pivot model?

From Dev

Laravel PIVOT table with extra fields

From Dev

How to use additional fields in pivot table Laravel

From Dev

Update fields in a pivot table Laravel 5.5

From Dev

Model missing all fields in Django Admin

From Dev

Laravel pivot: Get model from withPivot()

From Dev

How to Call function on laravel pivot model

From Dev

Laravel can I use pivot table as a model

From Dev

Laravel and pivot table to relate different model

From Dev

Laravel Attach/Detach model from pivot depending on pivot extra field

From Dev

Laravel custom authentication user fields

From Dev

Return calculated custom attribute from Laravel Pivot

From Dev

Laravel Custom Pivot Table relationship and eager loading?

From Dev

Laravel 5.2 Many to many with custom pivot

From Dev

json django model with custom fields representation

From Dev

Editing custom fields in Devise User model

From Dev

MVC Architecture - Adding fields with custom logic to the Model

From Dev

Automatically connect signals to custom Django model fields

From Dev

Adding Custom Data fields to an excel Pivot Table Using EPPlus

From Dev

Laravel Custom Route Model Binding

From Dev

Create model in a custom path in laravel

From Dev

Laravel - Model::create() works, but is missing attributes

From Java

Laravel, sync() - how to sync an array and also pass additional pivot fields?

From Dev

How do I access extra fields in a Laravel 5 pivot table?

From Dev

How to sync additional fields in pivot table [Laravel 5]

From Dev

Laravel how to get list of related fields through pivot table

From Dev

Laravel PIVOT table with extra fields and withCount('table') not working as expected

From Dev

Laravel Eloquent: Rename fields only in Model Class

From Dev

Laravel eloquent search on fields of related model

Related Related

  1. 1

    Laravel: How do I create a custom pivot model?

  2. 2

    Laravel PIVOT table with extra fields

  3. 3

    How to use additional fields in pivot table Laravel

  4. 4

    Update fields in a pivot table Laravel 5.5

  5. 5

    Model missing all fields in Django Admin

  6. 6

    Laravel pivot: Get model from withPivot()

  7. 7

    How to Call function on laravel pivot model

  8. 8

    Laravel can I use pivot table as a model

  9. 9

    Laravel and pivot table to relate different model

  10. 10

    Laravel Attach/Detach model from pivot depending on pivot extra field

  11. 11

    Laravel custom authentication user fields

  12. 12

    Return calculated custom attribute from Laravel Pivot

  13. 13

    Laravel Custom Pivot Table relationship and eager loading?

  14. 14

    Laravel 5.2 Many to many with custom pivot

  15. 15

    json django model with custom fields representation

  16. 16

    Editing custom fields in Devise User model

  17. 17

    MVC Architecture - Adding fields with custom logic to the Model

  18. 18

    Automatically connect signals to custom Django model fields

  19. 19

    Adding Custom Data fields to an excel Pivot Table Using EPPlus

  20. 20

    Laravel Custom Route Model Binding

  21. 21

    Create model in a custom path in laravel

  22. 22

    Laravel - Model::create() works, but is missing attributes

  23. 23

    Laravel, sync() - how to sync an array and also pass additional pivot fields?

  24. 24

    How do I access extra fields in a Laravel 5 pivot table?

  25. 25

    How to sync additional fields in pivot table [Laravel 5]

  26. 26

    Laravel how to get list of related fields through pivot table

  27. 27

    Laravel PIVOT table with extra fields and withCount('table') not working as expected

  28. 28

    Laravel Eloquent: Rename fields only in Model Class

  29. 29

    Laravel eloquent search on fields of related model

HotTag

Archive