2

我有车辆类 morphTo(Bus) 和 MorphTo(SUV) 和 MorphTo(Sedan)。

在 VehicleController 中保存时,我调用了 vehicleRepositoryInterface、vehicleRepository。

如何保存巴士、SUV 或轿车数据?

我的车辆类

Class Vehicle controller{
    public function get_create(){
     if($this->vehicle->create(Input::all()))
          return Redirect::to()
    }
          return Redirect::to()->withInput()->withErrors($this->vehicle->getMessages());
}

我的车辆存储库类如何处理带有 morphTo 关系的 if(Vehicle Type is Bus / SUV / Van)?

Class VehicleRepository Implements VehicleRepositoryInterface{
    public function create($array $inputs){
         if($this->validatator->IsValid($inputs)){
              // How to implement BUS or SUV or Van in here
                $this->model->create($inputs);
                return true;
         } 
        return false; 
    }
}
4

1 回答 1

0

最后,我自己回答了多态关系以保存在存储库中。

在创建函数中——

  if($this->validator->isValid($inputs){
      // first store in temp model. $this model will be destroyed.
       $temp_model = $this->model->create($inputs);
      //then Depency Injection comein for polymorphic model
       $this->polymorphicmodel->create($subinputs):
       // link with polymorphic relation
       $this->polymorphicmodel->modelable()->save($temp_model);
       return true;
    }
   return false;

非常感谢先进的修改或建议,以便更好地接近。

于 2014-10-04T02:50:15.163 回答