简体   繁体   中英

laravel select all row from 3 table

What could I do to select all column 1 row from 3 table in laravel 5.4 ?

I want to select posts, comments , users where post_id = comment.post_id and comment.user_id = user.id .

show it in single page post with post_title , Comment By username

I has 3 table like this

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

thank for your answer.

You can easily do that using Eloquent relationships lazy loading of data.

Best place to start is to read https://laravel.com/docs/5.4/eloquent-relationships

You can do something like:

$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();

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