Laravel滔滔不绝地向很多人回报空洞

时间:2016-02-09 13:42:33

标签: php laravel eloquent repository-pattern

因此,我试图在我的应用程序中使用雄辩的多对多关系。

我有三张表格如下

   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                |                |
    +---------------+----------------------------+------+-----+---------------------+----------------+

在我的用户存储库中,我有以下

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');
    }
}

我返回了一个显示父类和相关类的对象,但我实际上没有返回我的用户地址。有什么东西我不见了吗?

1 个答案:

答案 0 :(得分:1)

Alexrussell在他的评论中提出了一些你可以解决的问题,但我相信你的直接问题是在你的回归线末端缺少->get()

如果没有它,您将被要求调用您的方法:

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

由于hasManyThrough将返回Illuminate/Database/Eloquent/Relations/HasManyThrough的实例而非实际结果

供参考: https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Relations/HasManyThrough.html

请注意此处的示例: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');
    }
}

示例用法包括get():$roles = App\User::find(1)->roles()->orderBy('name')->get();

希望这有帮助