我有MorphTo(公交车)、MorphTo(越野车)和MorphTo(轿车)。
在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());
}
我的车辆仓库类如何处理if(车辆类型为Bus / SUV / Van)与morphTo关系?
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;
}
}
发布于 2014-10-04 10:50:15
最后,我自己得到了多态关系的答案,并将其保存在存储库中。
在创建函数中--
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;
真的很欣赏先进的修改或建议,以便更好地处理。
https://stackoverflow.com/questions/26131305
复制相似问题