dimanche 30 août 2015

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.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1EusY2B
via IFTTT

Aucun commentaire:

Enregistrer un commentaire