![](/img/trans.png)
[英]how to get all comments with their post title linked to posts table in laravel?
[英]get all posts with their comments from database in laravel
我有一個系統,其中有users
創建帖子,他們也可以評論posts
。
以下是遷移:
users
表
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('gender')->default('Male');
$table->string('email')->unique();
$table->string('city')->default('Peshawar');
$table->string('avatar')->default('user.png');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
posts
表
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('p_id');
$table->text('description');
$table->integer('user_id'); // this should be user id nothing else
$table->timestamps();
});
}
comments
表
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id'); // who has done comment
$table->integer('post_id'); // on which post comment has been done
$table->text('body');
$table->timestamps();
});
}
楷模
User
模型
class User extends Authenticatable{
use Notifiable;
// custom function used for relationshisp
// user can have many posts
public function posts(){
return $this->hasMany('App\Post');
}
// user can have many comments as well
public function comments(){
return $this->hasMany('App\Comment');
}
}
Post
模型
class Post extends Model{
// posts belongs to one user
public function user(){
return $this->belongsTo('App\User');
}
public function comments(){
return $this->hasMany('App\Comment');
}
}
Comment
模型
class Comment extends Model
{
// doing this we could insert data into comment table
protected $fillable =['user_id','post_id','body'];
// we need two(2) relations here
// relation of comment with post
// relation of comment with user
// 1.評論屬於一個帖子
public function post(){
return $this->belongsTo('App\Post');
}
// 2.評論也屬於用戶
public function user(){
return $this->belongsTo('App\User');
}
}
route
Route::get('/home', 'HomeController@index')->name('home');
這是HomeController
的index
方法
public function index(){
$posts = Post::with('comments')->get();
// $comments = Comment::all();
return view('home')->with( array('posts'=>$posts ) );
}
問題
因此,當我訪問/home
我希望獲得所有帖子的評論 。
正如您所看到的,我已經使用他們的comment
檢索posts
,如下所示:
$posts = Post::with('comments')->get();
然后我將它傳遞給home.blade.php
。 以下是我在home.blade.php
上所做的home.blade.php
來完成這項任務。
查看 /home.blade.php
@foreach($posts as $post)
<h4 class="media-heading">{{$post->user->name}}</h4>
<small>{{$post->created_at}}</small>
<p>{{$post->description}}</p>
<!-- trying to retrieve comments with each post -->
@if (count($post->comments))
@foreach($post->commnents as $comment)
<small>$comment->body</small>
@endforeach
@else
No comments Found
@endif
@endforeach
它給了我這個錯誤
未定義的變量:post(查看:F:\\ application \\ resources \\ views \\ home.blade.php)
記住這些models
及其relationships
,我是否以錯誤的方式為每個帖子檢索評論? 如果是這樣,我怎么能得到他們的comments
所有的posts
,當沒有comments
它應該說no comments found
。
以下是dd($posts)
dd($ posts)返回結果的結果,看一下注釋字段,即為空。
它可能是因為這個..
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('p_id'); <---
$table->text('description');
$table->integer('user_id'); // this should be user id nothing else
$table->timestamps();
});
}
您使用p_id而不是id的任何原因? 關系工作,以便post的id匹配到comments表中的post_id ..如果你使用這個你必須在創建關系時指定這個自定義鍵。
看看https://laravel.com/docs/5.8/eloquent-relationships#defining-relationships
嘗試將您的帖子自定義鍵作為第三個參數傳遞給關系:
評論模型
public function post(){
return $this->belongsTo('App\Post', 'post_id', 'p_id');
}
也許更好的選擇是,將表格的關鍵字改為'id'以符合Laravel的慣例。 創建一個新的遷移並運行它。
Schema::table('posts', function (Blueprint $table) {
$table->renameColumn('p_id', 'id');
});
另一種選擇是創建外鍵約束,以強制在數據庫級別引用完整性:
帖子表
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('p_id');
$table->text('description');
$table->integer('user_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
評論表
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->integer('post_id')->unsigned();
$table->text('body');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('post_id')->references('p_id')->on('posts');
});
}
您必須回滾並重新運行遷移(您將丟失保存在DB中的數據)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.