2015-08-31 63 views
1

我有我的控制器這不是簡單的代碼:如何使緊湊的代碼Laravel 5個雄辯關係

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')); 
    } 

... 

此代碼對我模式之一:

namespace App; 

use Illuminate\Auth\Authenticatable; 
use Illuminate\Database\Eloquent\Model; 
use Illuminate\Auth\Passwords\CanResetPassword; 
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; 
use Illuminate\Contracts\Auth\CanResetPassword as 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'); 
    } 

... 

這上的代碼查看

<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> 

正如你在我的控制器看,我呼籲在模型中的每個函數(返回查看( 'users.show',緊湊的( 'UserDetail', '用戶', 'UserSex', '省'));)來顯示之間的數據表格之間的數據關係

我做這樣的代碼沒有錯誤,而且運行良好。

我的問題是,我做對了嗎(基於Laravel 5)?

因爲我覺得這個方法並不簡單不緊湊,如果我做了很多表的更新版本。我還沒有探索所有的laravel功能。也許你們中的一些人可以幫助我做正確的事。

+0

我覺得我在我的控制器上犯了一個錯誤** **顯示約($ id)的參數功能。 現在這讓我更加困惑。 – wahyueka31

+0

你把UserSex和省放入不同的模型的原因是什麼? – yangqi

+0

@yangqi沒有理由將它們分開,我只是在官方的Laravel 5.1文檔中提到:http://laravel.com/docs/5.1/eloquent-relationships。我錯了嗎? – wahyueka31

回答

1

所有的方法&變量名應該是駱駝首先看到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()); 
    } 

你也可以使用get_defined_vars。這會將所有定義的 變量納入範圍&將通過它來查看。

而且改變你的方法名駱駝

namespace App; 

use Illuminate\Auth\Authenticatable; 
use Illuminate\Database\Eloquent\Model; 
use Illuminate\Auth\Passwords\CanResetPassword; 
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; 
use Illuminate\Contracts\Auth\CanResetPassword as 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'); 
    } 

更改變量視圖

<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> 

關注PSR-2 — Coding Style Guide

也可以嘗試用預先加載,因爲它解決查詢n + 1問題 http://laravel.com/docs/5.1/eloquent-relationships#eager-loading & 限制你的選擇

我寧願寫在你的控制器此代碼爲$user變量

$user = User::with(['userDetail' => function($q){ 
    $q->select(['id', 'firstName', 'lastName']); 
}, 'userSex' => function($q){ 
    $q->select(['id', 'gender']); 
}])->findOrFail($id); 

,並在您的視圖

<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> 

總是限制你SE LECT &不必要的加入,因爲這可能是昂貴的

+0

Hei ** @ ARIF MAHMUD RANA **,您的控制器腳本非常棒。非常感謝。 – wahyueka31

0

嘗試使用預先加載的車型啓動起來,讓你的控制器看起來就像這樣:

$user=User::find($id)->with('UserDetail')->with('UserSex')->with('Province'); 

return view('users.show')->with('user',$user); 

這將大幅增加你的應用程序的性能。

僅供參考Eloqouent - Eager Loading

+0

您的代碼給了我這個錯誤**「ErrorException in ca765b7a3065caff5ad7ab75ad6380bb line 15: 未定義的屬性:Illuminate \ Database \ Eloquent \ Builder :: $ username(查看:C:\ wamp \ www \ bookstore \ resources \ views \ users \ show (** $ username **在我的視圖**「{!! $ user-> username !!}」**)我不認爲我的表格列是使用它定義的(** $ username **是在我的視圖**「{!! $ user-> username !!}」**) – wahyueka31