【问题标题】:Laravel $posts->comments() isn't working hereLaravel $posts->comments() 在这里不起作用
【发布时间】:2014-06-11 17:44:37
【问题描述】:

这是我的模型中的代码:

models/Book.php

类书扩展 \Eloquent {

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();
}

}

models/Post.php

Class Post 扩展 Eloquent {

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();
}

}

models/Comment.php

类注释扩展了 Eloquent {

function post() {
    return $this->belongsTo('Post');
}

function user() {
    return $this->belongsTo('User');
}

}

但在我的UserController.php 中,我无法访问我的评论。我试过这些代码:

public function show($id)
{
        $user = User::find($id);

        $posts = Post::whereUserId($user->id)
                  ->orderBy('created_at','deac')->get();

        $comments = $posts->comments();
        dd($comments);
}

这总是说:

如何在帖子下访问我的 cmets?我也不认为$this->posts()->comments()->delete(); 不起作用。

【问题讨论】:

  • 您是否只想获取特定用户的评论?

标签: php laravel laravel-4 eloquent


【解决方案1】:

像这样在User 模型中添加一个关系:

// User model
public function books()
{
    return $this->hasMany('Book');
}

然后在你的controller:

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);
}

然后在你的view:

{{ $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: User (object)
           ->name (Property of User Model)
           ->email (Property of User Model) and more properties
           -> books (property of User Model, collection of Book Models
              -> [0] Book (has properties)
              -> [1] Book (has properties
                 -> posts (property of [1]Book, collection of Post Models
                 -> [0] Post (has properties)
                 -> [1] Post (has properties
                    -> comments (property of [1]Post, collection of Comment Models
                    -> [0] Comment (has properties)
                    -> [1] Comment (has properties

【讨论】:

  • 在我的User 模型上:public function books() { return $this->hasMany('Book'); } 已经存在,让我试试控制器。
  • return View::make('viewName')->with('book', $book); 说:未定义的变量:书
  • 那么,booksuser 模型中?
  • 糟糕!我的错误,它必须是 with('user', $user) 而不是 with('book', $book) :-)
  • 试试这个$user = User::with('books.posts' => function($postsQuery){ $postsQuery::with('comments' => function($commentsQuery){ $commentsQuery->orderBy('created_at', 'desc'); })->orderBy('created_at', 'desc'); })->find($id);
猜你喜欢
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 2013-05-16
  • 2019-02-10
  • 2017-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多