私は最良の方法は、別のテーブルに投稿を格納することだと思います。たとえば、次の移行
Schema::create('posts', function(Blueprint $table){
$table->increments('id');
$table->string('title');
$table->timestamps();
});
を使用して、ポスト・テーブルは、今すぐ移行を使用して、コメントテーブルを作成します:
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');
});
次のようにこれら2つのテーブル間の多くの関係1つを作成します。Post
については
をモデル、
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');
}
}
は、2つのデータベースのテーブルを移入した後:posts
とcomments
、あなたは、あなたのコントローラ内でそれらを別々に照会することができます。
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');
}
を次のようにそのコントローラ、クエリの投稿や各ほとんどのコメントにあなたの選択のいずれかの方法で、今
use App\Post;
use App\Comment;
: はそれを行うには、お使いのコントローラの上部にある2行を追加します。
私はそれを短くしようとしましたが、私の答えは長いです。それが助けてくれたらと思います。