Search code examples
laraveleloquentrelationships

Laravel / Eloquent - Many to many pivot model with morphToMany relation


I have a manyToMany relationship set up and have modelled the pivot table also. Also, in the pivot model I have a morphToMany relationship set up.

Here is a diagrammatic representation of the setup.

diagram

The trouble I am having is that I can't pull in the joins attribute on the pivot model.

I have this code in my Dimension model;

return $this->belongsToMany('Datasource', 'dimension_datasource')->withPivot('joins');

But I get this error: Unknown column dimension_datasource.joins

I have tried setting up an accessor on the pivot model but it appears that it is being ignored as I get the same error.

Thanks,

Geoff


Solution

  • This is working now. I'm distressed to admit I am not 100% sure why, but I'm fairly certain it was to do with the character encoding of backslashes in the fully qualified class names in the joinable_type column of the joinables table. So, if you are having the same issue, try looking there (there are indications elsewhere on the internet that Laravel should be escaping those backslashes and also vague suggestion of server settings being relevant).

    Sorry I can't be more specific!!