I have a pivot table that connects users to workspaces. On the pivot table, I also have a column for role, which defines the users role for that workspace. Can I provide Accessor (Getter) & Mutator (Setter) methods on the role inside the pivot table? I have been trying to look all over, but details on pivot tables in eloquent are pretty sparse.
I am not sure if I have to setup a custom pivot model? If I do, an example would be awesome as the documentation on pivot models is very basic.
Thanks.
Its impossible to use setters, will not affect pivot table... make the change in the controller instead.
If all you need to do is access additional fields on the pivot table, you just need to use the
withPivot()
method on the relationship definition:Now your role field will be available on the pivot table:
If you really need to create accessors/mutators for your pivot table, you will need to create a custom pivot table class. I have not done this before, so I don't know if this will actually work, but it looks like you would do this:
Create a new pivot class that contains your accessors/mutators. This class should extend the default Pivot class. This new class is the class that is going to get instantiated when User or Workspace creates a Pivot model instance.
Now, update your User and Workspace models to create this new pivot table class, instead of the default one. This is done by overriding the
newPivot()
method provided by the Model class. You want to override this method so that you create an instance of your new UserWorkspacePivot class, instead of the default Pivot class.This is a difficult question. The solutions I can think of are smelly and may cause some problems later on.
I am going to extend on Patricus's answer to make it work.
I was going to comment on Patricus's answer but there is simply too much to explain. To make his solution work with
attach
andsync
we must do some ugly things.The Problem
First let's identify the problem with his solution. His getters and setters do work but the belongsToMany relationship doesn't use the Pivot model when running
sync
,attach
, ordetach
. This means every time we call one of these with the$attributes
parameter the non-mutated data will be put into the database column.We could just try to remember that every time we call one of these we can't use the second parameter to attach the mutated data and just call
updateExistingPivot
with the data that must be mutated. So anattach
would be what Patricus stated:and we could never use the correct way of passing the pivot attributes as the
attach
methods second parameter shown in the first example. This will result in more database statements and code rot because you must always remember not to do the normal way. You could run into serious problems later on if you assume every developer, or even yourself, will just know not to use theattach
method with the second parameter as it was intended.The Solution (untested and imperfect)
To be able to call
attach
with the mutator on the pivot columns you must do some crazy extending. I haven't tested this but it may get you on the right path if you feel like giving it a try. We must first create our own relationship class that extendsBelongsToMany
and implements our customattach
method:Now we have to make each
Model::belongsToMany
use our newUserWorkspaceBelongsToMany
class instead of the normalBelongsToMany
. We do this by mocking thebelongsToMany
in our User and Workspace class:As you can see, we are still calling the database more but we don't have to worry about someone calling
attach
with the pivot attributes and them not getting mutated.Now use that inside your models instead of the normal belongsToMany: