Php Laravel$posts->;注释()不是';我不在这里工作
以下是我的模型中的代码:Php Laravel$posts->;注释()不是';我不在这里工作,php,laravel,laravel-4,eloquent,Php,Laravel,Laravel 4,Eloquent,以下是我的模型中的代码: models/Book.php 类目扩展\有说服力{ function posts() { return $this->hasMany('Post'); } function user() { return $this->belongsTo('User'); } public function cover() { return $this->morphMany('Image', 'imageable'); } public
models/Book.php
类目扩展\有说服力{
function posts() {
return $this->hasMany('Post');
}
function user() {
return $this->belongsTo('User');
}
public function cover()
{
return $this->morphMany('Image', 'imageable');
}
public function delete()
{
$this->posts()->comments()->delete();
$this->posts()->delete();
$this->cover()->delete();
// delete the model
return parent::delete();
}
function book() {
return $this->belongsTo('Book');
}
function comments() {
return $this->hasMany('Comment');
}
function user() {
return $this->belongsTo('User');
}
public function delete()
{
$this->comments()->delete();
// delete the model
return parent::delete();
}
function post() {
return $this->belongsTo('Post');
}
function user() {
return $this->belongsTo('User');
}
}
models/Post.php
班上的帖子很有说服力{
function posts() {
return $this->hasMany('Post');
}
function user() {
return $this->belongsTo('User');
}
public function cover()
{
return $this->morphMany('Image', 'imageable');
}
public function delete()
{
$this->posts()->comments()->delete();
$this->posts()->delete();
$this->cover()->delete();
// delete the model
return parent::delete();
}
function book() {
return $this->belongsTo('Book');
}
function comments() {
return $this->hasMany('Comment');
}
function user() {
return $this->belongsTo('User');
}
public function delete()
{
$this->comments()->delete();
// delete the model
return parent::delete();
}
function post() {
return $this->belongsTo('Post');
}
function user() {
return $this->belongsTo('User');
}
}
models/Comment.php
类注释扩展了雄辩的{
function posts() {
return $this->hasMany('Post');
}
function user() {
return $this->belongsTo('User');
}
public function cover()
{
return $this->morphMany('Image', 'imageable');
}
public function delete()
{
$this->posts()->comments()->delete();
$this->posts()->delete();
$this->cover()->delete();
// delete the model
return parent::delete();
}
function book() {
return $this->belongsTo('Book');
}
function comments() {
return $this->hasMany('Comment');
}
function user() {
return $this->belongsTo('User');
}
public function delete()
{
$this->comments()->delete();
// delete the model
return parent::delete();
}
function post() {
return $this->belongsTo('Post');
}
function user() {
return $this->belongsTo('User');
}
}
但是在我的UserController.php
中,我无法访问我的评论。我尝试了以下代码:
这总是说:
如何访问我在帖子下的评论?而且我不认为
$this->posts()->comments()->delete()代码>不起作用。在您的用户
模型中添加如下关系:
// User model
public function books()
{
return $this->hasMany('Book');
}
然后在您的控制器中
:
public function show($id)
{
// User->hasMany:books->hasMany:posts->hasMany:Comments
$user = User::with('books.posts.comments')->find($id);
return View::make('viewName')->with('user', $user);
}
然后在视图中
:
{{ $user->nameMaybe }}
{{ $user->emailMaybe }}
@foreach($user->books as $book)
{{ $book->title }}
{{ $book->author }}
@foreach($book->posts as $post)
{{ $post->slugMaybe }}
{{ $post->postTitleMaybe }}
@foreach($post->comments as $comment)
{{ $comment->commenttextMaybe }}
@endforeach
@endforeach
@endforeach
返回的用户
模型的结构如下(根据您的关系):
您是否试图只获取特定用户的评论?在我的User
model:public function books(){return$this->hasMany('Book');}
已经存在,让我用控制器试试。return View::make('viewName')->with('Book',$Book)代码>显示:未定义变量:bookSo,您在用户
模型中有书籍
?哎呀!我的错误是,它必须是with('user',$user)
而不是with('book',$book)
:-)试试这个$user=user::with('books.posts'=>函数($postsQuery::with('comments'=>函数($commentsQuery){$commentsQuery->orderBy('created_at','desc')))->orderBy('created_at','desc'))->find($id)代码>。