【发布时间】:2017-11-11 07:51:49
【问题描述】:
我试图从两个数据库表中显示我的数据,但我收到错误消息为 foreach() 提供的参数无效。有人知道我如何解决这个错误吗?
注意:我是 laravel 新手
这是我的控制器:
$post = PostModel::find($post_id);
$comment = new CommentModel;
我的 PostModel:
public function comments() {
return $this->belongsTo('App\CommentModel');
}
我的评论模型:
public function post() {
return $this->belongsTo('App\PostModel');
}
我想在其中显示我的数据库模型的视图页面:
@foreach($post->comments as $comment)
<div class="comment">
<div class="author-info">
<div class="author-name">
<h4>{{ $comment->name }}</h4>
</div>
</div>
<div class="comment-content">
{{ $comment->comment }}
</div>
</div>
@endforeach
这是我的评论表
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->text('comment');
$table->boolean('approved');
$table->integer('post_id')->unsigned();
$table->timestamps();
});
Schema::table('comments', function ($table){
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});
}
public function down()
{
Schema::dropForeign(['post_id']);
Schema::drop('comments');
}
【问题讨论】:
标签: php laravel laravel-5 foreach laravel-5.2