Laravel: How do I create a custom pivot model?

Robin

I'm working on a project that has a many to many relationship between User and Club. This relationship works and I can get the respective objects with: $user->clubs. The pivot table I've named memberships. I can get the pivot data with $club->pivot. Foreign keys are defined for the memberships table in migrations.

However, I'd like the pivot table to be represented by a model so that I can easily update attributes of Membership such as role (Or even add a Role model to Membership!) or status.

I've looked at "Defining A Custom Pivot Model" in the docs, but what it says doesn't work for me, I get:

ErrorException Argument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array, object given

I've also looked at this description of how to do it, but it's more or less the same as above.

Membership model:

class Membership extends Eloquent {

protected $table = 'memberships';

public function user()
{
    return $this->belongsTo('User');
}

public function club()
{
    return $this->belongsTo('Club');
}
}

Has anyone done this before?

Robin

This has been solved by a suggestion by a reddit user to extend Pivot in my Membership model. I also had to add ->withPivot('status', 'role')->withTimestamps() to the relationship declarations in the User and Club models.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I add a custom field to a model in Laravel?

From Dev

How do I get the current model from custom method in laravel

From Dev

how do I create custom error page in laravel 5

From Dev

How do I get the `pivot table` model to trigger the save/saved model events when attach or detach are called in Laravel?

From Dev

How do I get the `pivot table` model to trigger the save/saved model events when attach or detach are called in Laravel?

From Dev

Laravel Custom Pivot Model is missing fields

From Dev

In AngularJS, How do I create a custom validation directive that takes a $scope variable and compares it for equality to the ng-model?

From Dev

In AngularJS, How do I create a custom validation directive that takes a $scope variable and compares it for equality to the ng-model?

From Dev

How do I create a model of a function?

From Dev

Create model in a custom path in laravel

From Dev

How do I call a model in Laravel 5?

From Dev

How do I call model function on create event? Laravel-5

From Dev

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

From Dev

How do I query a polymorphic pivot table with Eloquent & Laravel 5

From Dev

Laravel can I use pivot table as a model

From Dev

How do I create a custom element in dart?

From Dev

How do I create a custom nonlinear filter?

From Dev

How do I create a custom array in powershell?

From Dev

Mass assignment on self relationship in Laravel (do I need to create model?)

From Dev

How do I create a model that "belongsTo" a "hasMany through" join model?

From Dev

How to Call function on laravel pivot model

From Dev

How do I create a custom cursor with a custom hotspot in wxPython?

From Dev

In CakePHP3 how do I create a custom model rule which validates that a time is after another time in the same table?

From Dev

How can I mock the create method on a model in laravel 5?

From Dev

How can I mock the create method on a model in laravel 5?

From Dev

How I can create Model from collection? Laravel 5.5

From Dev

How do I add user defined custom fields to a model?

From Dev

How do I call a custom method on a model with a template in Django?

From Dev

How do I add a custom method to a backbone model?

Related Related

  1. 1

    How do I add a custom field to a model in Laravel?

  2. 2

    How do I get the current model from custom method in laravel

  3. 3

    how do I create custom error page in laravel 5

  4. 4

    How do I get the `pivot table` model to trigger the save/saved model events when attach or detach are called in Laravel?

  5. 5

    How do I get the `pivot table` model to trigger the save/saved model events when attach or detach are called in Laravel?

  6. 6

    Laravel Custom Pivot Model is missing fields

  7. 7

    In AngularJS, How do I create a custom validation directive that takes a $scope variable and compares it for equality to the ng-model?

  8. 8

    In AngularJS, How do I create a custom validation directive that takes a $scope variable and compares it for equality to the ng-model?

  9. 9

    How do I create a model of a function?

  10. 10

    Create model in a custom path in laravel

  11. 11

    How do I call a model in Laravel 5?

  12. 12

    How do I call model function on create event? Laravel-5

  13. 13

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

  14. 14

    How do I query a polymorphic pivot table with Eloquent & Laravel 5

  15. 15

    Laravel can I use pivot table as a model

  16. 16

    How do I create a custom element in dart?

  17. 17

    How do I create a custom nonlinear filter?

  18. 18

    How do I create a custom array in powershell?

  19. 19

    Mass assignment on self relationship in Laravel (do I need to create model?)

  20. 20

    How do I create a model that "belongsTo" a "hasMany through" join model?

  21. 21

    How to Call function on laravel pivot model

  22. 22

    How do I create a custom cursor with a custom hotspot in wxPython?

  23. 23

    In CakePHP3 how do I create a custom model rule which validates that a time is after another time in the same table?

  24. 24

    How can I mock the create method on a model in laravel 5?

  25. 25

    How can I mock the create method on a model in laravel 5?

  26. 26

    How I can create Model from collection? Laravel 5.5

  27. 27

    How do I add user defined custom fields to a model?

  28. 28

    How do I call a custom method on a model with a template in Django?

  29. 29

    How do I add a custom method to a backbone model?

HotTag

Archive