dongpo0409 2016-02-09 13:42
浏览 24

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

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?

  • 写回答

1条回答 默认 最新

  • donglangtun1850 2016-02-09 16:54
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀