简体   繁体   中英

How to make Compact code Laravel 5 Eloquent Relationships

I have this not simple code on my 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 one of my Model :

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

...

And this 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 you see in my controller , I call every function in model ( return view('users.show', compact('UserDetail', 'User', 'UserSex', 'Province')); ) to show the data between table in Eloquent Relationships .

I have no error doing code like this and this run well.

My question is, am I doing it right (base on Laravel 5)?

Because I think this method is not simple , not compact if I make a lot of table later. I still have not explored all the laravel features. Maybe some of you can help me make it right.

First of all your method & variable names should be 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 you can use get_defined_vars . This will get all your defined variables into the scope & will pass it to view.

Also change your method names to camel case

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

Change your variables in the 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 as it solves query n+1 problem http://laravel.com/docs/5.1/eloquent-relationships#eager-loading & limit your select

I would rather write this code in your controller for $user variable

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

and in your 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 your select & unnecessary joins as this can be expensive

Try using Eager Loading to boot up the models together, so that your controller will look like this:

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

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

It will drastically increase the performance of your application.

For reference Eloqouent - Eager Loading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM