简体   繁体   English

Laravel雄辩的多对多返回为空

[英]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. Alexrussell在他的评论中指出了一些可以解决的优点,但是我认为您的直接问题是在返回行末尾缺少->get()

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 由于hasManyThrough将返回Illuminate/Database/Eloquent/Relations/HasManyThrough的实例, Illuminate/Database/Eloquent/Relations/HasManyThrough不是实际结果

For reference: https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Relations/HasManyThrough.html 供参考: 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 请注意此处的示例: 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(); 示例用法包括get(): $roles = App\\User::find(1)->roles()->orderBy('name')->get();

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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