سؤال

after googling and banging my head against the desk for hours I was still unable to solve my various issues with querying relationships the way I want to.

Problem: I have a one-to-many relationship between Article and Comment and Comment and Reply, which works out just fine. In one case though I want to get JUST the Comments + Replies without loading the inverse relationship to Article. How would that be achieved?

I have already given up hope on my original endeavor of loading specific cells from the Article associated to a certain comment. That seems to be even more impossible.

Is my thinking wrong or what's the problem here?

Here are the models:

Article:

class Article extends Eloquent{

     protected $table = 'articles';

     protected $softDelete = true;

     protected $fillable = array('short_title','long_title','description','content','zenra_link','source_url','source_title');

     public static $rules = array(
         'short_title'=>'required|min:5',
         'long_title' =>'required',
         'description'=>'required',
         'content'=>'required'
     );

     public function category(){
         return $this->belongsTo('Category');
     }
     public function tag(){
         return $this->belongsToMany('Tag','tagmaps');
     }
     public function comment(){
         return $this->hasMany('Comment');
     }
     public function picture(){
         return $this->hasOne('Picture');
     }
 }

Comment:

 class Comment extends Eloquent{

      protected $fillable = array('author','content','published','article_id');
      protected $table = 'comment';
      public static $rules = array(
        'author'=>'required|min:5',
        'comment' =>'required|min:10'
      );

      public function reply(){
         return $this->hasMany('Reply');
      }
      public function article(){
         return $this->belongsTo('Article');
      }
 }

And last but not least the Replies:

 class Reply extends Eloquent{

    protected $fillable = array('author','content','published','comment_id');
    protected $table = 'reply';
    public static $rules = array(
       'author'=>'required|min:5',
       'comment' =>'required|min:5'
    );

    public function comment(){
        return $this->belongsTo('Comment');
    }

 }

Question 1: How do I get JUST the comments + replies by comment ID?

Question 2: If possible, can I get comments + replies + specific cell from article by comment ID?

Question 3: How do I get JUST the comment without ANYTHING attached to it by comment ID?

Thank you.

هل كانت مفيدة؟

المحلول

3. Comment::find($commentId); // return Eloquent Model with given id

2. // I suggest renaming relation to replies as there are many replies for a comment
   $comment = Comment::with('replies') // load related replies
      ->with('article') // you want a single field, but better load whole article, then fetch what needed
      ->find($commentId);

   // then:
   $comment->replies; // Collection of related Reply models
   $comment->article->id; // id of related article (or whatever field you want)

1. Comment::with('replies')->find($commentId); // retrieves comment with related replies
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top