Php 拉威尔未定义偏移误差
我刚刚用laravel完成了我的博客,我想在上面添加一个评论功能,但我几乎没有遇到这样的错误,有人能帮我吗??, 对不起,我的英语,英语不是我的母语, 谢谢:) 这是我的AdminBlog.php模型Php 拉威尔未定义偏移误差,php,laravel,Php,Laravel,我刚刚用laravel完成了我的博客,我想在上面添加一个评论功能,但我几乎没有遇到这样的错误,有人能帮我吗??, 对不起,我的英语,英语不是我的母语, 谢谢:) 这是我的AdminBlog.php模型 <?php namespace App; use Illuminate\Database\Eloquent\Model; class AdminBlog extends Model { protected $table = 'admin'; protected $fill
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AdminBlog extends Model
{
protected $table = 'admin';
protected $fillable = ['title','text','images','slug'];
public function comment(){
return $this->hasMany('App\Comment');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $table = 'comment';
protected $fillable = ['name','email','text','post_id'];
public function post(){
return $this->belongsTo('App\AdminBlog');
}
}
您应该使用:
<div class="col-md-12 post-comment-show">
@foreach($post->comment as $list)
<p>{{ $list->text }}</p>
@foreach
</div>
试试下面的方法
$post->comment()->get()
或
调用与()
的关系时,它将返回查询生成器的实例,并允许您筛选对象。如果在不使用()
的情况下删除调用它,它将返回一个可访问数组的集合
另一个建议如果你有很多意见,你应该把你的关系命名为复数
在这种情况下:
public function comments(){
return $this->hasMany('App\Comment');
}
// Use it like this
$post->comments
您需要更改显示功能:
public function show($id){
$post = AdminBlog::findOrFail($id);
$comment = Comment::all();
//dd($comment);
return view('blog/show', ['post' => $post,
'comment' => $comment]);
}
至
public function show($id){
$post = AdminBlog::with('comment')->where('admin.id',$id)->get();
return view('blog/show', compact('post'));
}
希望这项工作为你
$post->comment()->get()
$post->comment
public function comments(){
return $this->hasMany('App\Comment');
}
// Use it like this
$post->comments
public function show($id){
$post = AdminBlog::findOrFail($id);
$comment = Comment::all();
//dd($comment);
return view('blog/show', ['post' => $post,
'comment' => $comment]);
}
public function show($id){
$post = AdminBlog::with('comment')->where('admin.id',$id)->get();
return view('blog/show', compact('post'));
}