简体   繁体   中英

Laravel eloquent many to many returns empty

So i'm trying to get my head around using eloquent for many to many relationships in my application.

I have three tables as followed

   user
    +----------------+------------------+------+-----+---------------------+----------------+
    | Field          | Type             | Null | Key | Default             | Extra          |
    +----------------+------------------+------+-----+---------------------+----------------+
    | id             | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
    | first_name     | varchar(255)     | NO   |     | NULL                |                |
    | last_name      | varchar(255)     | NO   |     | NULL                |                |
    | email          | varchar(255)     | NO   | UNI | NULL                |                |
    | password       | varchar(60)      | NO   |     | NULL                |                |
    | remember_token | varchar(100)     | YES  |     | NULL                |                |
    | created_at     | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
    | updated_at     | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
    | active         | enum('yes','no') | NO   |     | NULL                |                |
    | last_login     | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
    +----------------+------------------+------+-----+---------------------+----------------+

    user_has_address
    +------------+------------------+------+-----+---------+-------+
    | Field      | Type             | Null | Key | Default | Extra |
    +------------+------------------+------+-----+---------+-------+
    | address_id | int(10) unsigned | YES  | MUL | NULL    |       |
    | users_id   | int(10) unsigned | YES  | MUL | NULL    |       |
    +------------+------------------+------+-----+---------+-------+

    address
    +---------------+----------------------------+------+-----+---------------------+----------------+
    | Field         | Type                       | Null | Key | Default             | Extra          |
    +---------------+----------------------------+------+-----+---------------------+----------------+
    | id            | int(10) unsigned           | NO   | PRI | NULL                | auto_increment |
    | name_number   | varchar(45)                | NO   |     | NULL                |                |
    | first_line    | varchar(45)                | NO   |     | NULL                |                |
    | second_line   | varchar(45)                | NO   |     | NULL                |                |
    | town_city     | varchar(45)                | NO   |     | NULL                |                |
    | state_country | varchar(45)                | NO   |     | NULL                |                |
    | post_zip      | varchar(45)                | NO   |     | NULL                |                |
    | type          | enum('delivery','billing') | NO   |     | NULL                |                |
    | created_at    | timestamp                  | NO   |     | 0000-00-00 00:00:00 |                |
    | updated_at    | timestamp                  | NO   |     | 0000-00-00 00:00:00 |                |
    | deleted_at    | timestamp                  | YES  |     | NULL                |                |
    +---------------+----------------------------+------+-----+---------------------+----------------+

in my user repository i have the following

namespace App\Libraries\Repositories\Core\Users;

use Schema;
use App\Models\Core\User;
use Bosnadev\Repositories\Eloquent\Repository;
use Symfony\Component\HttpKernel\Exception\HttpException;

class UserRepository extends Repository
{
    public function getUsersAddresses()
    {
        return $this->userModel->hasManyThrough('App\Models\Bundle\Addresses\Address','App\Models\Bundle\Addresses\UserHasAdress','id','address_id');
    }
}

Im returned an object that shows parent and related classes but im not actually returned my users address. Is there something im missing?

Alexrussell made some good points in his comment that you could possibly address, however I believe your immediate problem is a missing ->get() at the end of your return line.

Without it, you would be required to call your method like:

$repository->getUsersAddresses()->get();

As hasManyThrough will return an instance of Illuminate/Database/Eloquent/Relations/HasManyThrough not the actual results

For reference: https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Relations/HasManyThrough.html

Note in the example here: https://laravel.com/docs/5.1/eloquent-relationships#many-to-many

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function roles()
    {
        return $this->belongsToMany('App\Role');
    }
}

The example usage includes a get(): $roles = App\\User::find(1)->roles()->orderBy('name')->get();

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM