PHP [SOLVED]: Laravel- 5.5 AppComment::user must return a relationship instance.

PHP [SOLVED]: Laravel- 5.5 AppComment::user must return a relationship instance.

Home Forums Scripting PHP Tutorials PHP [SOLVED]: Laravel- 5.5 AppComment::user must return a relationship instance.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #245548

    Cloudy Point
    Keymaster

    QuestionQuestion

    I want to display products comments. But When I do that, it gives me above error. How can I fix that ?

    I’m using One To Many relationship beetwen product-comments and user-comments

    Product model;

     public function comments(){
            return $this->hasMany('AppComment','product_id','id');
        }
    

    User Model;

    public function comments() {
    
             return $this->hasMany('AppComment','user_id','id');
         }
    

    Comment Model;

    public function user(){
    
            $this->belongsTo('AppUser');
        }
    
        public function product(){
            $this->belongsTo('AppProduct');
        }
    

    Blade file

    <figcaption class="text-center">{{$comment->user->username}}</figcaption>
    

    #245549

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You need to return relationship. So add return to the user() relationship definition method:

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

    The same is with the product() relationship:

    public function product()
    {
        return $this->belongsTo('AppProduct');
    }
    

    Source: https://stackoverflow.com/questions/48028655/laravel-5-5-app-commentuser-must-return-a-relationship-instance
    Author: Alexey Mezenin
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.