self_id = $self_id; foreach ($remote_type as $type => $remote_model) { $this->remote_type[$type] = new $remote_model($this); } $this->remote_type_id = $remote_type_id; $this->model = $model; $this->self_type = $self_type; } /** * @param array $calls [\Closure(Model)] * @return $this */ public function each(array $calls) { foreach ($calls as $type => $call) { if (isset($this->remote_type[$type])) { $call->call($this->remote_type[$type]); } } return $this; } public function setRelation() { $type = $this->model[$this->self_type]; foreach ($this->remote_type as $type1 => $remote_model) { if ($type1 == $type) { $remote_model->where($this->remote_type_id[$type], $this->model[$this->self_id]); } else { unset($this->remote_type[$type1]); } } return $this; } public function setRelationList(ListModel $list_model) { $this->list_model = $list_model; $types = $this->list_model->pluck($this->self_type, true, true); foreach ($this->remote_type as $type => $remote_model) { if (isset($types[$type])) { $ids = array_column($types[$type], $this->self_id); $remote_model->whereIn($this->remote_type_id[$type], $ids); } else { unset($this->remote_type[$type]); } } return $this; } public function setRelationModel(Model $model) { $this->model = $model; return $this->setRelation(); } public function get() { return end($this->remote_type)->find(); } public function merge($key) { if ($this->list_model === null) { $this->model->$key = $this->get(); } else { $third_arr = $this->third_model->findAll()->pluck($this->third_column, true); foreach ($this->list_model as $val) { $k = $val[$this->self_column]; $val->$key = isset($third_arr[$k]) ? $third_arr[$k] : null; } } unset($this->model, $this->third_model); } }