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')代码>