如何通过多态关系向远处的模型编写关系

时间:2019-04-09 17:24:43

标签: php laravel eloquent polymorphism

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

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

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


代码如下:

  • 课程要求:
    • user_id
    • 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;

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以使用

通过用户请求获取用户请假和加班
$requests = $user->requests->with('requestable');

但这将获取所有不依赖于类型的用户请求,但是您可以根据类型通过使用“离开和超时”功能并在其中指定类型来获取它们

用户类别

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

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

答案 2 :(得分:0)

回答我自己的问题。

使用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); 
}