I'm making and inventory system for basic parts, that provides coverage for more than one company location. So my inventory
table has parts information such as part_number and description as well as a location_id
that foreign keys into a locations
table. I have several stock items with the same part number, but have varying inventory levels at each location. I want to group the inventory by location, and then list all of the inventory at that location before cycling through the other locations and doing the same thing.
Here is what I have in my controller:
$inventory = Inventory::where('part_number', '12345')->get();
// dd($inventory);
$inventory_grouped_by_location = $inventory->groupBy('location_id');
// dd($inventory_grouped_by_location);
return view('inventory.show', compact('inventory_grouped_by_location');
Here is what I have in my Inventory model:
public function location()
{
return $this->belongsTo(Location::class);
}
Here is what I have in my Location model:
public function parts_inventory()
{
return $this->hasMany(Inventory::class);
}
Here is what I have tried in my view:
@foreach($inventory_grouped_by_location as $location)
<p>Location: </p>
@foreach($location as $inventory)
<p>Part Number: </p>
@endforeach
@endforeach
The above code throws and error because I'm trying to access the location relationship, but I've only got the location->id
from the inventory table. I've tried to access the relationship like this $location[$key]->name
but no joy.
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Swcp1c
via IFTTT
Aucun commentaire:
Enregistrer un commentaire