Laravel Eloquent关系映射问题有三个表和belongsTo()函数

时间:2015-07-25 10:33:44

标签: php mysql laravel laravel-5 eloquent

我有三个表(实例,用户,user_instance)。每个用户都可以分配给一个实例。一个实例可以有许多不同的用户。

我尝试通过使用Laravel 5.1 Eloquent和belongsTo()函数,通过调用$ this-> user-> instance-> instance_id来获取特定用户所分配实例的instance_id。

无论我尝试过什么,我总是在我的AuthController.php中得到一个NULL结果:

数据库架构:

    mysql> show columns from instances;
    +------------+------------------+------+-----+---------+----------------+
    | Field      | Type             | Null | Key | Default | Extra          |
    +------------+------------------+------+-----+---------+----------------+
    | id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
    | name       | varchar(150)     | NO   |     | NULL    |                |
    | key        | varchar(150)     | NO   |     | NULL    |                |
    | created_at | int(11)          | NO   |     | NULL    |                |
    | updated_at | int(11)          | NO   |     | NULL    |                |
    +------------+------------------+------+-----+---------+----------------+

    mysql> show columns from users;
    +----------------+---------------------+------+-----+---------+----------------+
    | Field          | Type                | Null | Key | Default | Extra          |
    +----------------+---------------------+------+-----+---------+----------------+
    | id             | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
    | first_name     | varchar(50)         | YES  |     | NULL    |                |
    | last_name      | varchar(50)         | YES  |     | NULL    |                |
    | email          | varchar(100)        | NO   |     | NULL    |                |
    | password       | varchar(255)        | NO   |     | NULL    |                |
    | salt           | varchar(255)        | YES  |     | NULL    |                |
    | remember_token | varchar(255)        | YES  |     | NULL    |                |
    | created_at     | int(10) unsigned    | NO   |     | NULL    |                |
    | updated_at     | int(10)             | NO   |     | NULL    |                |
    | last_login     | int(10) unsigned    | YES  |     | NULL    |                |
    | ip_address     | varchar(15)         | NO   |     | NULL    |                |
    | timezone       | int(10)             | YES  |     | NULL    |                |
    | active         | tinyint(1) unsigned | YES  |     | NULL    |                |
    +----------------+---------------------+------+-----+---------+----------------+

    mysql> show columns from user_instance;
    +-------------+------------------+------+-----+---------+----------------+
    | Field       | Type             | Null | Key | Default | Extra          |
    +-------------+------------------+------+-----+---------+----------------+
    | id          | int(10) unsigned | YES  | PRI | NULL    | auto_increment |
    | user_id     | int(10) unsigned | NO   |     | NULL    |                |
    | instance_id | int(10) unsigned | NO   |     | NULL    |                |
    | created_at  | int(11)          | NO   |     | NULL    |                |
    | updated_at  | int(11)          | NO   |     | NULL    |                |
    +-------------+------------------+------+-----+---------+----------------+

应用/控制器/ AuthController.php

    <?php
    use App\Models\User;
    use App\Models\Instance;
    use App\Models\User_Instance;
    class AuthController extends Controller {
    ...
    die($this->user->instance); // Returns: NULL
    ...
    }
    ?>

应用/型号/ Instance.php

    <?php namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

    class Instance extends Model
    {
        /**
         * Indicates if the model should be timestamped.
         *
         * @var bool
         */
        public $timestamps = true;

        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'instances';

        /**
         * The tables primary key.
         *
         * @var string
         */
        protected $primaryKey = 'id';

        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = ['name', 'key', 'created_at', 'updated_at'];

        /**
         * The attributes excluded from the model's JSON form.
         *
         * @var array
         */
        protected $hidden = [];
    }
    ?>

应用/型号/ User_Instance.php

    <?php namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

    class User_Instance extends Model
    {
        /**
         * Indicates if the model should be timestamped.
         *
         * @var bool
         */
        public $timestamps = true;

        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'user_instance';

        /**
         * The tables primary key.
         *
         * @var string
         */
        protected $primaryKey = 'id';

        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = ['user_id', 'instance_id', 'created_at', 'updated_at'];

        /**
         * The attributes excluded from the model's JSON form.
         *
         * @var array
         */
        protected $hidden = [];
    }
    ?>

应用/型号/ user.php的

    <?php namespace App\Models;

    use Illuminate\Auth\Authenticatable;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Auth\Passwords\CanResetPassword;
    use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
    use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
    use Zizaco\Entrust\Traits\EntrustUserTrait;

    class User extends Model implements AuthenticatableContract, CanResetPasswordContract
    {
        use Authenticatable, CanResetPassword, EntrustUserTrait;

        /**
         * Indicates if the model should be timestamped.
         *
         * @var bool
         */
        public $timestamps = true;

        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'users';

        /**
         * The tables primary key.
         *
         * @var string
         */
        protected $primaryKey = 'id';

        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'first_name',
            'last_name',
            'email',
            'password',
            'salt',
            'remember_token',
            'created_at',
            'updated_at',
            'last_login',
            'ip_address',
            'timezone',
            'active'
            ];

        /**
         * The attributes excluded from the model's JSON form.
         *
         * @var array
         */
        protected $hidden = ['password', 'remember_token'];

        /**
         * Map the instance with the user.
         *
         * @var int
         */
        public function instance()
        {
            return $this->belongsTo('App\Models\User_Instance');
        }
    }
    ?>

1 个答案:

答案 0 :(得分:1)

public function instance()
{
 return $this->belongsTo('App\Models\User_Instance');
}

belongsTo,在上面的方法中,使用snakecase特别应用where条件(即instance_id)..但在你的情况下会导致错误的结果或根本没有结果(你想在user_id上映射)

正如Laravel - Eloquent所说。  如果父模型上的外键不是snakecase名称(instance_id),则可以将自定义键名作为第二个参数传递给belongsTo方法:

return $this->belongsTo('App\Models\User_Instance','user_id');

>>laravel relations