我認爲最好的方法是將帖子存儲在一張單獨的桌子上。
Schema::create('comments', function(Blueprint $table){
$table->increments('id');
$table->integer('post_id')->unsigned();
$table->string('name');
$table->text('comment_body');
$table->timestamps();
$table->foreign('poem_id')->references('id')->on('posts');
});
創建一個以這兩個表之間的許多關係如下:
對於Post
例如使用以下遷移
Schema::create('posts', function(Blueprint $table){
$table->increments('id');
$table->string('title');
$table->timestamps();
});
現在使用的遷移創建註釋表中創建一個職位表型號,
class Post extends Model
{
...
public function comments(){
return $this->hasMany('App\Comment');
}
}
和Comment
模型,
class Comment extends Model
{
protected $fillable = ['post_id', 'c_body', 'name'];
public function posts(){
return $this->belongsTo('App\Poem', 'post_id');
}
}
此時,填充兩個數據庫表後:posts
和comments
,您可以然後分別在控制器中對它們進行查詢。 要做到這一點,在控制器的頂部添加兩行: use App\Post;
use App\Comment;
現在您所選擇的任何方法在控制,查詢崗位和每個大多數意見如下
public function index(){
$posts = Post::where('published',true);
$comments = Post::where('published',true)->comments;
// pass this data to your view
return view('anyview', compact('posts', 'comments');
}
我的答案很長,儘管我試圖把它縮短。希望它有幫助。