2016-07-31 17 views
2

Ich bin neu in Laravel und habe mit diesem Problem für eine kurze Weile nachgedacht. Der Hauptgrund, den ich gerne eloquent verwenden möchte, besteht darin, dass die Datetime-Stamps funktionieren, da die DB-Methode die Felder created_at und updated_at ignoriert. Ich versuche, die folgende Abfrage eloquent zu reproduzieren:Laravel Konvertierung von DB-Abfrage zu eloquent

$user_results = DB::table('users')-> 
        leftJoin('roles', 'users.role_id','=', 'roles.id')-> 
        get(); 

ich das Setup

Benutzermigration Migration

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateUsersTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->increments('id')->unsigned(); 
      $table->integer('role_id')->unsigned()->index(); 
      $table->string('name'); 
      $table->string('email')->unique(); 
      $table->string('password'); 
      $table->rememberToken(); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('users'); 
    } 
} 

Rollen

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateRolesTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('roles', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('rolename'); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('roles'); 
    } 
} 

und Benutzermodell folgende Datenbank enthält

public function role() { 
    return $this->belongsTo('App\Role'); 
} 

Jede Hilfe würde sehr geschätzt werden.

danke.

Antwort

0

einfach können Sie DB :: Tabelle ('Benutzer') mit App \ User

App \ User :: leftJoin ('Rollen', 'users.role_id', '=', ‚roles.id ersetzen ') -> get();

+0

das war genau das, was ich suchte, aber es nicht herausfinden konnte. Danke eine Zillion. –