0
我想从两个数据库表中显示我的数据,并且我得到错误消息为foreach()提供的无效参数。任何人都知道我如何解决这个错误?Laravel 5.2为foreach提供的无效参数()
注:我在laravel
这里新是我的控制器:
$post = PostModel::find($post_id);
$comment = new CommentModel;
我PostModel:
public function comments() {
return $this->belongsTo('App\CommentModel');
}
我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');
}
兄弟,当我改变属于关联到的hasMany,然后创建新的错误 (1)未找到列:1054'where子句'中的未知列'comments.post_model_id'(SQL:从'comments'中选择count(*)作为聚合,其中'comments'.'post_model_id' = 1和'comments'.'post_model_id'不为null)(View:E:\ Project \ htdocs \ lara_blog \ resources \ views \ blog \ single.blade.php) (2)未找到列:1054'where子句'中的未知列'comments.post_model_id'(SQL:select count(*)as'comments'中的聚合,其中'comments'.'post_model_id '= 1和'comments'.'post_model_id'不为空) – Mahbub
这是因为您的外键名称与型号名称不匹配 - 您为什么需要该型号后缀?无论如何,我已经更新了答案 –
它的工作原理有很多......谢谢全部 – Mahbub