Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5.1雄辩的关系_Laravel_Laravel 5.1 - Fatal编程技术网

Laravel 5.1雄辩的关系

Laravel 5.1雄辩的关系,laravel,laravel-5.1,Laravel,Laravel 5.1,有没有人能从我基于截图和模型设置的雄辩关系中提出建议? 模型设置: class Leaves extends Model { protected $table = 'leaves'; protected $fillable = [ 'leave_type', 'user_id' ]; public function user() { return $this->belongsTo('App\User

有没有人能从我基于截图和模型设置的雄辩关系中提出建议?

模型设置:

class Leaves extends Model
{
   protected $table = 'leaves';

    protected $fillable = [
        'leave_type',
        'user_id'
    ];

    public function user()
    {
        return $this->belongsTo('App\User');
    }

}

class LeaveType extends Model
{
    protected $table = 'leave_type';

    protected $fillable = ['type_name'];
}

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

    protected $table = 'users';

    protected $fillable = ['name', 'email', 'password'];

    protected $hidden = ['password', 'remember_token'];

    public function leave()
    {
        return $this->hasMany('App\Leaves');
    }

}
目前我只能获取leave的详细信息,但需要根据

$user = User::oldest('name')->get();
foreach ($users as $user) {
     $user->leave()-get();

}
叶片型号:

class Leaves extends Model {
   protected $table = 'leaves';

    protected $fillable = [
        'leave_type',
        'user_id'
    ];

    public function User()
    {
        return $this->belongsTo('App\User');
    }
    public function LeaveType()
    {
        return $this->belongsTo('App\LeaveType');
    }

}
LeavesType型号:

class LeaveType extends Model {
    protected $table = 'leave_type';

    protected $fillable = ['type_name'];
    public function Leaves()
    {
        return $this->hasMany('App\Leaves');
    }
}
用户模型:

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

    protected $table = 'users';

    protected $fillable = ['name', 'email', 'password'];

    protected $hidden = ['password', 'remember_token'];

    public function Leaves()
    {
        return $this->hasMany('App\Leaves');
    }

}

在你的
离开
模式中,你会

function type() {
    return $this->hasOne(App\LeaveType);
}
在你的假期里,你应该报答

function leave() {
    return $this->belongsToMany(App\LeaveType);
}
在控制器中:

$user = User::oldest('name')->with('leave', 'leave.type')->get();
dd($user->leave->type);

还要注意,在获取属性时,在
->离开
->键入
之后缺少
()
leave()
将返回一个查询生成器,以便您可以执行
$user->leave()->创建($leave)我的函数类型()似乎有问题。我的外键引用有什么问题吗?从附件中可以看出,Leaves表中的leave_type是leave_type表的外来引用。对不起,laravel的默认值是
model_name_id
。在您的情况下,这将是
保留\u type\u id
,但您可以覆盖它。语法是
return$this->hasOne('App\Model\u Name','foreign\u key','local\u key')