0

I have this DB::table query, which it get list of branches, and what I need to do further is to get list of items for each branch as well, I can use join, but to respect optimization I was wondering if this can be achieved via loading relationship? I did some googling however, but did not find a satisfaction related response yet.

$branches = DB::table('branches')
->join('branche_main_info', 'branches.id', '=', 'branche_main_info.business_id')
->where('branche_main_info.status', 'approved');

And the relationship is:

// Relation ship with Items
public function ItemsList(){
    return $this->hasMany(Item::class);
}

Now what I am trying to get out of this is:

$branches->with('ItemsList')->get();

Related Models:

// Relationship with details table.
public function approvedItemDetails(){
    return $this->hasOne(ItemDetails::class, 'item_id')->where('details_status', 'approved')->latest();
}

// Relation ship with Items
public function ItemsList(){
    return $this->hasMany(Item::class);
}
Naser Nikzad
  • 713
  • 12
  • 27

0 Answers0