Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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
Php 如何通过多态关系将关系写入远程模型_Php_Laravel_Eloquent_Polymorphism - Fatal编程技术网

Php 如何通过多态关系将关系写入远程模型

Php 如何通过多态关系将关系写入远程模型,php,laravel,eloquent,polymorphism,Php,Laravel,Eloquent,Polymorphism,我有一个多态关系,其中类(请求)可以与类(休假)或类(加班)有关系 类(请求)的每个对象都属于一个用户 我想在用户类中设置一个关系,直接获取他们所有的休假或加班对象 下面是代码的外观: 课程要求与: 用户id 可请求的用户id 可请求的_类型可以是App\Leave或App\加班 类请求扩展模型 { 可请求的公共函数(){ 返回$this->morphTo(); } 公共函数用户(){ 返回$this->belongsTo('App\User'); } } 课假 假期扩展模型 {

我有一个多态关系,其中类(请求)可以与类(休假)或类(加班)有关系

类(请求)的每个对象都属于一个用户

我想在用户类中设置一个关系,直接获取他们所有的休假或加班对象


下面是代码的外观:

  • 课程要求与:
    • 用户id
    • 可请求的用户id
    • 可请求的_类型可以是App\Leave或App\加班
类请求扩展模型
{
可请求的公共函数(){
返回$this->morphTo();
}
公共函数用户(){
返回$this->belongsTo('App\User');
}
}
  • 课假
假期扩展模型
{
公共功能请求(){
返回$this->morphOne('App\Request','requestable');
}
}
  • 课余时间
类超时扩展模型
{
公共功能请求(){
返回$this->morphOne('App\Request','requestable');
}
}
  • 类用户
类用户扩展可验证性
{
公共职能要求(){
返回$this->hasMany(请求::类);
}
公共职能{
//这里需要帮助吗
}
公共功能超时(){
//这里呢
}
}

我想做的是获得用户的所有假期和超时,所以最终我应该能够做到:

$userLeaves=$user->leaves;
$userOvertimes=$user->overtimes;

似乎您需要多态关系(您已经定义)和hasManyThrough的组合。
return$this->hasManyThrough(Leave::class,Request::class)
return$this->hasManyThrough(加班::类,请求::类)

分别地但是检查外键和本地键(请参阅更多信息)。

似乎您需要多态关系(您已经定义)和hasManyThrough的组合。
return$this->hasManyThrough(Leave::class,Request::class)
return$this->hasManyThrough(加班::类,请求::类)

分别地但是请检查外键和本地键(请参阅更多信息)。

您可以使用

$requests = $user->requests->with('requestable');
但是,这将获取不依赖于类型的所有用户请求,但是如果需要并在其中指定类型,可以使用leaves和overtimes函数获取依赖于类型的用户请求

用户类

public function leaves()
{
    return $this->requests->where('requestable_type', 'App\Leave');
}

public function overTimes()
{
    return $this->requests->where('requestable_type', 'App\OverTime');
}

您可以通过使用获取用户请求中的用户假期和超时

$requests = $user->requests->with('requestable');
但是,这将获取不依赖于类型的所有用户请求,但是如果需要并在其中指定类型,可以使用leaves和overtimes函数获取依赖于类型的用户请求

用户类

public function leaves()
{
    return $this->requests->where('requestable_type', 'App\Leave');
}

public function overTimes()
{
    return $this->requests->where('requestable_type', 'App\OverTime');
}

回答我自己的问题

使用
hasManyThrough

public function leaves() {
    return $this->hasManyThrough(
        Leave::class, // the class that we want objects from
        Request::class, // the class sitting between this one and the target
        'user_id', // this class's foreign key in the request class
        'id', // foreign key in leave class
        'id', // local key in this class
        'requestable_id' // key of the leave in the request class
        )
        // we have to limit it to only Leave class
        ->where('requestable_type', array_search(Leave::class, Relation::morphMap()) ?: Leave::class);
}

public function overtimes() {
    return $this->hasManyThrough(
        Overtime::class, // the class that we want objects from
        Request::class, // the class sitting between this one and the target
        'user_id', // this class's foreign key in the request class
        'id', // foreign key in overtime class
        'id', // local key in this class
        'requestable_id' // key of the overtime in the request class
        )
        // we have to limit it to only overtime class
        ->where('requestable_type', array_search(Overtime::class, Relation::morphMap()) ?: Overtime::class); 
}

回答我自己的问题

使用
hasManyThrough

public function leaves() {
    return $this->hasManyThrough(
        Leave::class, // the class that we want objects from
        Request::class, // the class sitting between this one and the target
        'user_id', // this class's foreign key in the request class
        'id', // foreign key in leave class
        'id', // local key in this class
        'requestable_id' // key of the leave in the request class
        )
        // we have to limit it to only Leave class
        ->where('requestable_type', array_search(Leave::class, Relation::morphMap()) ?: Leave::class);
}

public function overtimes() {
    return $this->hasManyThrough(
        Overtime::class, // the class that we want objects from
        Request::class, // the class sitting between this one and the target
        'user_id', // this class's foreign key in the request class
        'id', // foreign key in overtime class
        'id', // local key in this class
        'requestable_id' // key of the overtime in the request class
        )
        // we have to limit it to only overtime class
        ->where('requestable_type', array_search(Overtime::class, Relation::morphMap()) ?: Overtime::class); 
}

你要找的就是这种关系吗。或者可以使用->hasMany()->where();你能试一试吗?我认为它不适用于多形关系。这种关系有很多你想要的东西。或者可以使用->hasMany()->where();你能试一试吗?我认为它不适用于polymorphic关系,但这将返回类型为Request的对象,而不是leave或overtimes的对象,但这将返回类型为Request的对象,而不是leave或overtimeYes的对象,我相信hasManyThrough是解决方案,但无论我使用foregin/本地键尝试了多少组合,我都无法让它工作,可以尝试一下吗?我从未使用过这种关系。也许您需要指定相关多态模型的类型。尝试使用。是的,我相信hasManyThrough是解决方案,但无论我使用foregin/本地键尝试了多少组合,我都无法让它工作,可以尝试一下吗?我从未使用过这种关系。也许您需要指定相关多态模型的类型。请尝试使用。这将向其他用户显示问题已解决。请。这会向其他用户显示问题已解决。