繁体   English   中英

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

[英]How to write a relationship to a distant model through a polymorphic relation

我有一个多态关系,其中类(请求)可以与类(离开)或类(超时)有关系。

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

我想在User类中设置一个关系,以直接获取其所有的Leave或Overtime对象。


代码如下所示:

  • 课堂要求:
    • 用户身份
    • requestable_id
    • requestable_type 可以是App \\ Leave或App \\ Overtime
    class Request extends Model
    {
        public function requestable() {
            return $this->morphTo();
        }

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

  • 上课假
    class Leave extends Model
    {
        public function request() {
            return $this->morphOne('App\Request', 'requestable');
        }
    }

  • 班级加班
    class Overtime extends Model
    {
        public function request() {
            return $this->morphOne('App\Request', 'requestable');
        }
    }

  • 班级用户
    class User extends Authenticatable
    {
        public function requests() {
            return $this->hasMany(Request::class);
        }

        public function leaves() {
            // Need help here
        }

        public function overtimes() {
            // And here
        }
    }


我想做的就是让用户拥有所有的假期和加班时间,因此最终我应该能够做到这一点:

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

似乎您需要多态关系(已经定义)和hasManyThrough的组合。 return $this->hasManyThrough(Leave::class, Request::class); return $this->hasManyThrough(Overtime::class, Request::class); 分别。 但是请检查外键和本地键( 在此处查看更多信息)。

您可以使用来通过用户请求获取用户休假和加班

$requests = $user->requests->with('requestable');

但这将获取不依赖于类型的所有用户请求,但是,如果需要,您可以通过使用leaves and 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); 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM