首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >laravel 4如何实现多态关系的存储库

laravel 4如何实现多态关系的存储库
EN

Stack Overflow用户
提问于 2014-10-01 06:37:16
回答 1查看 227关注 0票数 2

我有MorphTo(公交车)、MorphTo(越野车)和MorphTo(轿车)。

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

如何保存公交车、SUV或轿车数据?

我的车辆类

代码语言:javascript
运行
复制
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关系?

代码语言:javascript
运行
复制
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; 
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-10-04 10:50:15

最后,我自己得到了多态关系的答案,并将其保存在存储库中。

在创建函数中--

代码语言:javascript
运行
复制
  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;

真的很欣赏先进的修改或建议,以便更好地处理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26131305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档