簡體   English   中英

laravel從3表中選擇所有行

[英]laravel select all row from 3 table

我怎樣做才能從Laravel 5.4的3表中選擇所有第1列的行?

我想選擇帖子,評論,用戶,其中post_id = comment.post_idcomment.user_id = user.id

在帶有post_title單頁帖子中顯示,按用戶名評論

我有3張桌子

Schema::create('posts', function (Blueprint $table) {
      $table->increments('id');
      $table->text('title');
      $table->longText('description');
      $table->text('image');
      $table->timestamps();
});

Schema::create('comments', function (Blueprint $table) {
      $table->increments('id');
      $table->longText('comment');
      $table->integer('user_id');
      $table->integer('post_id');
      $table->timestamps();
});

Schema::create('users',function (Blueprint $table){
      $table->increments('id');
      $table->string('username',30)->unique();
      $table->string('email')->unique();
      $table->string('password',60);
      $table->rememberToken();
      $table->timestamps();
});

感謝您的回答。

您可以使用雄辯的關系輕松地延遲加載數據。

最好的起點是閱讀https://laravel.com/docs/5.4/eloquent-relationships

您可以執行以下操作:

$items = \DB::table('post')
->leftJoin('comment','comment.post_id', '=', 'post.post_id')
->leftJoin('user','user.user_id', '=', 'comment.user_id')
->where('user.user_id',$user_id)
->get();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM