2016-11-18 103 views
0

如何從使用數據透視表連接的2個表中獲取數據?例如,在我的情況下,我使用透視表(penulis表)將用戶表連接到日誌表。現在我想獲取屬於特定用戶的日記數據。我試過這個:從使用數據透視表連接的2個表中獲取數據(Laravel 5.3)

$journal_list = DB::table('journal')->where('id_user', '=','id_journal')->orderBy('id', 'desc')->paginate(20); 

上面的代碼不起作用。下面是我的遷移:

用戶表:

public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name'); 
      $table->string('username')->unique(); 
      $table->string('userslug'); 
      $table->string('nameslug'); 
      $table->string('email')->unique(); 
      $table->string('phone')->nullable(); 
      $table->string('address')->nullable(); 
      $table->string('password'); 
      $table->rememberToken(); 
      $table->enum('level', ['admin', 'author']); 
      $table->timestamps(); 
     }); 
    } 

期刊表:

public function up() { 
     Schema::create('journal', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('title', 255); 
      $table->text('abstract'); 
      $table->text('file'); 
      $table->integer('id_edition')->unsigned(); 
      $table->string('journalslug'); 
      $table->timestamps(); 
     }); 
    } 

Penulis表(數據透視表)

public function up() 
    { 
     Schema::create('penulis', function (Blueprint $table) { 
      // Create tabel penulis 
      $table->integer('id_user')->unsigned()->index(); 
      $table->integer('id_journal')->unsigned()->index(); 
      $table->timestamps(); 

      // Set PK 
      $table->primary(['id_user', 'id_journal']); 

      // Set FK penulis --- user 
      $table->foreign('id_user') 
        ->references('id') 
        ->on('users') 
        ->onDelete('cascade') 
        ->onUpdate('cascade'); 

      // Set FK penulis --- journal 
      $table->foreign('id_journal') 
        ->references('id') 
        ->on('journal') 
        ->onDelete('cascade') 
        ->onUpdate('cascade'); 
     }); 
    } 

的View Composer:

public function boot() 
    { 
     View::composer('user/show', function ($view) { 
      $journal_list = User::where('id', $user_id)->with('journal')->first(); 
      $view->with('journal_list', $journal_list); 
     }); 
    } 
+0

您已將我的答案中的代碼複製到View Composer部分,但您從未說過它是否適用於您。那麼,它解決了這個問題嗎?如果沒有,你有任何錯誤? –

+0

給了我錯誤「未定義的變量用戶ID」。我不想使用雄辯,我想通過視圖編輯器傳遞數據。我更新了視圖作曲家的問題@Alexey –

回答

1

如果你想用雄辯的,您應該設置belongsToMany()關係第一:

class User extends Authenticatable 
{ 

    public function journals() 
    { 
     return $this->belongsToMany('App\Journal'); 
    } 

然後使用預先加載來加載數據:

User::where('id', $userId)->with('journals')->first(); 

如果你不想用雄辯和只需要日誌:

$journal_ids = DB::table('penulis')->where('id_user', $userId)->get(['id_journal'])->toArray(); 
$journal_list = DB::table('journal')->whereIn('id', $journal_ids)->paginate(20); 

或者使用join()來做同樣的事情。

+0

嘗試了這個,給了我錯誤「未定義的變量userID」。我不想使用雄辯,我想通過視圖編輯器傳遞數據。我已經用視圖作曲家 –

+0

@AriandoMiller更新了這個問題,要傳遞數據,您需要首先獲取數據。如果你想得到'未定義的變量userID'錯誤信息,只要將它設置爲'$ userId = 5'或類似'$ userId = auth() - > user() - > id'。如果您不想使用Eloquent,請嘗試使用我的答案中的非Eloquent代碼。 –

+0

我嘗試了兩種方法,但仍未顯示屬於特定用戶的日誌數據。也許我搞砸了用戶ID?我不想獲得當前的用戶ID,但選擇了用戶ID,例如,如果我點擊Jon Doe的個人資料,它會顯示Jon Doe的聯繫人數據以及屬於他的期刊 –

0

爲什麼,如果你正在使用Laravel

首先定義你的模型

class User extends Model { 

    public function journal(){ 
     return $this->belongsToMany('App\Journal', 'penulis', 'id_user', 'id_journal'); 
    } 

} 


class Journal extends Model { 

    public function user(){ 
     return $this->belongsToMany('App\User', 'penulis', 'id_journal', 'id_user'); 
    } 

} 

當然的relations你需要與table name定義模型中的tablefillables沒有利用Eloquent和分別爲table columns

並獲得相關users可以用雄辯的具體數據,

User::with('journal')->paginate(20); 

這將加載20 users(paginated)與相關journal數據。

要進一步瞭解雄辯的關係,請看看這個link

希望它能解決您的問題。

+0

我完全一樣,仍然它不起作用 –

+0

@AriandoMiller你有什麼錯誤嗎? –

+0

否,但相關數據不會顯示 –

相關問題