lundi 25 février 2019

unable to fetch the details from related tables in laravel (using with)

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:

pName:null uName:null rest of the things is printing fine

thanks in advance




Aucun commentaire:

Enregistrer un commentaire