-
Notifications
You must be signed in to change notification settings - Fork 107
Problem with belongsToMany relationship #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@adiachenko I don't think this is a problem with this package. The package calls the methods on your model class, so it uses the relationships objects that are returned by those methods. I wouldn't recommend putting the model classes in the JSON API namespaces. This is because Eloquent does a lot of "magic" based on the class names of models, and all your models will be called "Model". Generally I put all the parts of constructing a JSON API within those JSON API namespaces - as basically JSON API is a "view" layer - i.e. a JSON representation of your application's models. When it comes to the Eloquent models, I'd follow the Laravel convention and have them in a namespace (normally the "App" namespace), with each class called it's relevant name. In your example it should be You can see that in the demo app here: Models are in the |
@lindyhopchris Thank you for quick response. I'll try following different conventions to avoid unexpected problems with Eloquent then. |
Uh oh!
There was an error while loading. Please reload this page.
Note, that I am currently using
0.5.x-dev
(as I'm working with Laravel 5.3) so the problem below may not apply to earlier versions of this package.BelongsToMany relationships leads to the following Exception.
At first glance, it seems like the package can't figure out the name of pivot table and foreign key in accordance with Laravel naming conventions.
When I manually specify names (i.e.
return $this->belongsToMany(\App\JsonApi\Roles\Model::class, 'role_user', 'id', 'role_id')->withTimestamps();
), Exception is no longer thrown but I encounter another issue:$resource->roles
should be a collection with multiple(!) items but I get the collection with only the very first one in response.Below is the code that lead to this post.
Model
Schema
The text was updated successfully, but these errors were encountered: