0
votes

am trying to fetch userName from user table and productName from products table in my ProductReview.php but its returning null

ProductReview.php

class ProductReview extends Model
{
    protected $table = 'product_review';

    protected $fillable = ['review_id','user_id','order_id','product_id','region','vendor','verified_order',
                               'star_rating','date_of_review','review_title','review_message','published',
                               'relevance','order_id','order_item_id','order_type','order_number','source_of_review',
                               'created_at','updated_at','published_by'];

public function pName(){
    return $this->hasOne('App\Models\Product','product_id');
    }
    public function uName(){
        return $this->hasOne('App\Models\User','id');
    }



Function getAllAdminReviews($data){

  $reviews = ProductReview::select('product_review.*,'product_id','user_id')
                      ->with(['pName' => function($pr){
                                $pr->select('product_name');      
                            },
                           'uName' => function($un){
                                $un->select('user_name');        
                            } 
                          ]);
return $reviews;
        }

        }

Product.php

public function pName(){
     return $this->belongsTo('App\Models\ProductReview','product_id');
}

User.php

public function uName(){
    return $this->belongsTo('App\Models\ProductReview','id');
}

output: product_id:586, user_id:123 pName:null uName:null rest of the things is printing fine

relations

Products(product_id,product_name), Users(id,user_name), product_reviews(review_id,product_id,user_id,review_message)

so i need to fetch just user_name from Users table and product_name from Products table based on the id's of table product_reviews(user_id,product_id) but its giving null.i hope my question is clear

2

2 Answers

0
votes
$reviews = ProductReview::with(['pName', 'uName'])->get();

Now above will give you product reviews with product and user.

To add selection of columns you can use relation:foreign_key,column to select a specific column. This is kind of a need if you are adding selection in a relational eager loading, you need to select the foreign key.

So you can do :

$reviews = ProductReview::with('pName:product_id,product_name')->with( 'uName:user_id,user_name')->get();

Please check as per your database if these foreign keys are correct

0
votes

the problem was i wasn't passing the id's of the child table, correct implementation is

ProductReview.php

 public function productName(){
        return $this->hasOne('App\Models\Product','product_id','product_id');
    }
    public function userName(){
        return $this->hasOne('App\Models\User','id','user_id');
    }


 $reviews = ProductReview::with(['productName' => function($pr){
            $pr->select('product_name','product_id');      
            },
             'userName' => function($un){
                 $un->select('fname','id');        
          } 
      ]);