Repository pattern (Laravel) switching beetwen models -


i'm writing app in laravel using repository pattern. main idea write 1 repository handle 2 models based on route parameters.

so routes.php looks this:

routes.php

<?php $api = app('dingo\api\routing\router');   $api->version('v1', function ($api) {     $api->post('create/{type}', ['uses' => 'beyondi\account\http\controllers\accountcontroller@insert']);  }); 

accountcontroller.php

class accountcontroller extends controller {      /**      * generate json web token.      */     protected $account;      public function __construct(typerepositoryinterface $user)     {         $this->account = $user;     }       public function insert(accountrequest $request)     {         if($this->account->create($request->input()))         {             return $this->response->array("user inserted")->setstatuscode(200);         }else         {             return $this->response->array("error, user not inserted")->setstatuscode(500);         }     } 

accountrequest.php

class accountrequest extends request {     public function __construct(formrequest $request)     {          if ($request->route('type')=='consumer')         {             $rules=[                 'looking_for' => 'required|in:male,female,trans',                 'photo' => 'required',                 'newsletter' => 'boolean|required'             ];             $this->rules($rules);         }else         {             $rules=[                 'type' => 'required|in:private,agency',                 'sexuality' => 'required|in:hetero,bi,homo',                 'height' => 'required|numeric'             ];             $this->rules($rules);          }     }      public function rules($rules)     {          return [$rules];     }      public function authorize()             {                 return true;             }         } 

typerepositoryinterface.php

interface typerepositoryinterface {     public function create(array $data);      public function update($id, array $data);      public function delete($id);      public function getall(array $columns = ['*']);      public function findbyid($id, $columns = ['*']);  } 

abstractrepository.php

abstract class abstractrepository implements abstractrepositoryinterface {     protected $model;      /**      * create new repository interface      *      * @param model $model      */     public function __construct(model $model)     {         $this->model = $model;     }      /**      * new instance of model      *      * @param array $attributes      * @return static      */     public function getnew(array $attributes = [])     {         return $this->model->newinstance($attributes);     }      /**      * insert new data in database      *      * @param array $data      * @return static      */     public function create(array $data)     {         return $this->model->create($data);     }      /**      * update data in database      *      * @param $id      * @param array $data      * @return mixed      */     public function update($id, array $data)     {         return $this->model->whereid($id)->update($data);     }      /**      * delete data database      *      * @param $id      * @return bool|null      * @throws \exception      */     public function delete($id)     {         return $this->model->delete($id);     }      /**      * data database      *      * @param array $columns      * @return \illuminate\database\eloquent\collection|static[]      */     public function getall(array $columns = ['*'])     {         return $this->model->all($columns);     }      /**      * find data id      *      * @param $id      * @param array $columns      * @return \illuminate\support\collection|null|static      */     public function findbyid($id, $columns = ['*'])     {         return $this->model->find($id, $columns);     }   } 

typerepository.php

class typerepository extends abstractrepository implements typerepositoryinterface {     protected $model;      /**      * create new repository interface      *      * @param model $model      */     public function __construct(providermodel $providermodel,consumermodel $consumermodel,request $request, $type)     {         if ($request->route('type')=='consumer')         {             $this->model = $consumermodel;         }else         {             $this->model = $providermodel;         }      }   } 

as can see trying switch between 2 models in typerepository based on route {type} parameter.

unresolvable dependency resolving [parameter #3 [ <required> $type ]] in class account\\repositories\\type\\eloquent\\typerepository 

error. missing? how correctly?

you not type-hinting $type parameter of typerepository constructor service container can't instantiate it.

i suggest removing $type parameter , using instead:

$type = $request->input('type'); 

inside constructor.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -