dtcd27183 2019-04-09 17:24
浏览 61
已采纳

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

I have a polymorphic relation where class (Request) can have a relationship with either class (Leave) or class (Overtime).

Each object of class (Request) belongs to a user.

I would like to setup a relationship in the User class to directly get all of their Leave or Overtime objects.


Here is how the code looks:

  • Class Request with:
    • user_id
    • requestable_id
    • requestable_type can be App\Leave or App\Overtime
    class Request extends Model
    {
        public function requestable() {
            return $this->morphTo();
        }

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

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

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

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

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

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


What I would like to do is get all leaves and overtimes a user has, so ultimately I should be able to do this:

    $userLeaves = $user->leaves;
    $userOvertimes = $user->overtimes;
  • 写回答

3条回答 默认 最新

  • dpb75177 2019-04-10 10:17
    关注

    Answering my own question.

    Using 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); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi