Skip to content

Problem with belongsToMany relationship #21

Closed
@adiachenko

Description

@adiachenko

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.

Next Illuminate\Database\QueryException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'helios.model_model' doesn't exist (SQL: select `roles`.*, `model_model`.`model_id` as `pivot_model_id`, `model_model`.`created_at` as `pivot_created_at`, `model_model`.`updated_at` as `pivot_updated_at` from `roles` inner join `model_model` on `roles`.`id` = `model_model`.`model_id` where `model_model`.`model_id` = 1) in /var/www/helios/vendor/laravel/framework/src/Illuminate/Database/Connection.php:761

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

<?php

namespace App\JsonApi\Users;

use Illuminate\Database\Eloquent\Model as EloquentModel;

class Model extends EloquentModel
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function roles()
    {
        return $this->belongsToMany(\App\JsonApi\Roles\Model::class)->withTimestamps();
    }
}

Schema

<?php

namespace App\JsonApi\Users;

use CloudCreativity\LaravelJsonApi\Schema\EloquentSchema;

class Schema extends EloquentSchema
{
    const RESOURCE_TYPE = 'users';

    /**
     * @var string
     */
    protected $resourceType = self::RESOURCE_TYPE;

    /**
     * Whether resource member names are hyphenated
     *
     * @var bool
     */
    protected $hyphenated = false;

    /**
     * The model attribute keys to serialize.
     *
     * @var array
     */
    protected $attributes = [
        'email',
        'first_name',
        'last_name',
    ];

    /**
     * Get resource links.
     *
     * @param object $resource
     * @param bool   $isPrimary
     * @param array  $includeRelationships
     * @return array
     */
    public function getRelationships($resource, $isPrimary, array $includeRelationships)
    {
        return [
            'roles' => [
                self::SHOW_SELF => true,
                self::SHOW_RELATED => true,
                self::DATA => $resource->roles,
            ],
        ];
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions