how can i join multiple tables in eloquent using relationships?

Viewed 42

How should i join these 4 tables users, pages, category, products using relationships intead in controller.I also prefer calling these functions from corresponding models using respective function names but not database column names in view.

These are my models:

User model:

class User extends Authenticatable{ 
    use HasFactory, Notifiable;

    protected $fillable = ['id','name' ];
    protected $hidden = [ 'password', 'remember_token', ];
    protected $casts = ['email_verified_at' => 'datetime', ];

    public function getuserName(){
        if($this->name){ return "$this->name"; } 
        return null;
    }
    public function page(){
        return $this->hasMany('App\Models\Pages', 'user_id');
    }
    public function product(){
        return $this->hasMany('App\Models\Products', 'user_id');
    }
}

Products model:

class products extends Model {
  use HasFactory;
  protected $table = 'products';
  protected $fillable = [  'id', 'user_id', 'parent_page_id',  'pcategory_id', 'prodname', ];
  /**get id(PK)****/
   public function fetchid(){
      if($this->id){ return $this->id; } 
      return null;
   }
  /**get pageid (FK)****/
  public function fetchpageid(){
     if($this->parent_page_id){ return $this->parent_page_id; } 
    return null;
  }
   /**get categoryid (FK)****/
  public function fetchcategoryid(){
     if($this->pcategory_id){ return $this->pcategory_id; } 
    return null;
  }
  /**get product owner****/
    public function fetchuser_id(){
     if($this->user_id){ return $this->user_id;   } 
     return null;
   }
  /**get product name****/
   public function fetchprodname(){
    if($this->prodname){ return $this->prodname; } 
    return null;
  }
    /**pick session userid from User model****/
  public function user(){
   return $this->belongsTo('App\Models\User', 'user_id');
   }
}

Post model:

 
class pages extends Model {
  use HasFactory;
  protected $table = 'pages';
  protected $fillable = [  'page_id', 'user_id', 'pagename', ];
  /**get page id(PK)****/
   public function fetchpage_id(){
     if($this->page_id){ return $this->page_id; } 
       return null;
    }
  /**get page owner****/
   public function fetchuser_id(){
     if($this->user_id){ return $this->user_id; } 
      return null;
    }
  /**get page name****/
   public function fetchpagename(){
     if($this->pagename){ return $this->pagename; } 
        return null;
   }
    /**pick session userid from User model****/
  public function user(){
      return $this->belongsTo('App\Models\User', 'user_id');
   }
}

Category model:

class category extends Model {
   use HasFactory;
   protected $table = 'category';
   protected $fillable = [  'category_id', 'name', ];
    /**get category id(PK)****/
    public function fetchcategory_id(){
     if($this->category_id){ return $this->category_id; } 
        return null;
        }
    /**get category name****/
    public function fetchcname(){
     if($this->name){ return $this->name; } 
       return null;
        }
} 

How should i join these 4 tables users, pages, category, products using relationships intead

$carousel = Products::where(function($querycarousel ) {
    return $querycarousel 
      ->leftJoin('users', 'products.user_id', 'users.id')
      ->leftJoin('pages', 'products.parent_page_id', 'pages.page_id')
       ->leftJoin('category', 'products.pcategory_id', 'category.category_id')
           ->where('products.verification_status', 'Verified')
           ->where('category.name', 'New');
     })->inRandomOrder()
                ->limit(50)
                        ->get();
  return view('pages.home')->with('carousel ', $carousel );

View I also prefer calling these functions from corresponding models using respective function names but not database column names

     @foreach ($carousel->chunk(5) as $key => $batchresults)
        @foreach ($batchresults as $key => $feed)
            <div>
                <p>{{ $feed->fetchprodname() }}</p>
                <p>{{ $feed->fetchcname() }}</p>
                <p>{{ $feed->fetchpagename() }}</p>
                <p>{{ $feed->getuserName() }}</p>
          </div>
        @endforeach
    @endforeach
0 Answers
Related