php - How to make Compact code Laravel 5 Eloquent Relationships -
i have not simple code on controller:
namespace app\http\controllers; use illuminate\http\request; use app\user; use app\userdetail; use app\usersex; use app\province; use app\http\requests; use app\http\controllers\controller; class usercontroller extends controller { ... public function show($id) { // $user = user::find($id); $userdetail = user::find($id)->userdetail; $usersex = user::find($id)->usersex; $province = user::find($id)->province; return view('users.show', compact('userdetail', 'user', 'usersex', 'province')); } ...
this code on 1 of model:
namespace app; use illuminate\auth\authenticatable; use illuminate\database\eloquent\model; use illuminate\auth\passwords\canresetpassword; use illuminate\contracts\auth\authenticatable authenticatablecontract; use illuminate\contracts\auth\canresetpassword canresetpasswordcontract; class user extends model implements authenticatablecontract, canresetpasswordcontract { ... protected $hidden = ['password', 'remember_token']; public $timestamps = false; public function userdetail() { return $this->hasone('app\userdetail', 'userdetail_id'); } public function usersex() { return $this->hasone('app\usersex', 'sex_id'); } public function province() { return $this->hasone('app\province', 'province_id'); } ...
and code on view:
<div class="form-group"> <label for="isbn" class="col-sm-2 control-label">user name</label> <div class="col-sm-10"> <input type="text" class="form-control" id="isbn" placeholder="{!! $user->username !!}" readonly> </div> </div> <div class="form-group"> <label for="title" class="col-sm-2 control-label">full name</label> <div class="col-sm-10"> <input type="text" class="form-control" id="firstname" placeholder="{!! $userdetail->firstname !!} {!! $userdetail->lastname !!}" readonly> </div> </div> <div class="form-group"> <label for="publisher" class="col-sm-2 control-label">sex</label> <div class="col-sm-10"> <input type="text" class="form-control" id="sex" placeholder="{!! $usersex->gender !!}" readonly> </div> </div>
as see in controller, call every function in model (return view('users.show', compact('userdetail', 'user', 'usersex', 'province'));) show data between table in eloquent relationships.
i have no error doing code , run well.
my question is, doing right (base on laravel 5)?
because think method not simple, not compact if make lot of table later. still have not explored laravel features. maybe of can me make right.
first of method & variable names should camel case see http://www.php-fig.org/psr/psr-1/#4-2-properties & http://www.php-fig.org/psr/psr-2/#4-3-methods
namespace app\http\controllers; use illuminate\http\request; use app\user; use app\userdetail; use app\usersex; use app\province; use app\http\requests; use app\http\controllers\controller; class usercontroller extends controller { ... public function show($id) { // $user = user::find($id); $userdetail = user::find($id)->userdetail; $usersex = user::find($id)->usersex; $province = user::find($id)->province; return view('users.show', get_defined_vars()); }
also can use get_defined_vars. defined variables scope & pass view.
also change method names camel case
namespace app; use illuminate\auth\authenticatable; use illuminate\database\eloquent\model; use illuminate\auth\passwords\canresetpassword; use illuminate\contracts\auth\authenticatable authenticatablecontract; use illuminate\contracts\auth\canresetpassword canresetpasswordcontract; class user extends model implements authenticatablecontract, canresetpasswordcontract { ... protected $hidden = ['password', 'remember_token']; public $timestamps = false; public function userdetail() { return $this->hasone('app\userdetail', 'userdetail_id'); } public function usersex() { return $this->hasone('app\usersex', 'sex_id'); } public function province() { return $this->hasone('app\province', 'province_id'); }
change variables in view
<div class="form-group"> <label for="isbn" class="col-sm-2 control-label">user name</label> <div class="col-sm-10"> <input type="text" class="form-control" id="isbn" placeholder="{!! $user->username !!}" readonly> </div> </div> <div class="form-group"> <label for="title" class="col-sm-2 control-label">full name</label> <div class="col-sm-10"> <input type="text" class="form-control" id="firstname" placeholder="{!! $userdetail->firstname !!} {!! $userdetail->lastname !!}" readonly> </div> </div> <div class="form-group"> <label for="publisher" class="col-sm-2 control-label">sex</label> <div class="col-sm-10"> <input type="text" class="form-control" id="sex" placeholder="{!! $usersex->gender !!}" readonly> </div> </div>
follow psr-2 — coding style guide
also try use eager loading solves query n+1 problem http://laravel.com/docs/5.1/eloquent-relationships#eager-loading & limit select
i rather write code in controller $user
variable
$user = user::with(['userdetail' => function($q){ $q->select(['id', 'firstname', 'lastname']); }, 'usersex' => function($q){ $q->select(['id', 'gender']); }])->findorfail($id);
and in view
<div class="form-group"> <label for="isbn" class="col-sm-2 control-label">user name</label> <div class="col-sm-10"> <input type="text" class="form-control" id="isbn" placeholder="{!! $user->username !!}" readonly> </div> </div> <div class="form-group"> <label for="title" class="col-sm-2 control-label">full name</label> <div class="col-sm-10"> <input type="text" class="form-control" id="firstname" placeholder="{!! $user->userdetail->firstname !!} {!! $user->userdetail->lastname !!}" readonly> </div> </div> <div class="form-group"> <label for="publisher" class="col-sm-2 control-label">sex</label> <div class="col-sm-10"> <input type="text" class="form-control" id="sex" placeholder="{!! $user->usersex->gender !!}" readonly> </div> </div>
always limit select & unnecessary joins can expensive
Comments
Post a Comment