Berke
Berke

Reputation: 760

Laravel Proper Relationship

So i have a 3 tables. Users-Inventory-Item. User hasmany Items, so far i have no problem. I can reach Items of User with this relationship.

Item table contains : user_id, item_id

So my problem is to reach item info of the Inventory of the User by item_id(which will find id of Item table and bring the info). I couldn't manage it, I need you advices about which relations would be the proper for my aim.

Models

class User extends Authenticatable
{
    public function Inventory(){
        return $this->hasMany('App/Item');
    }

class UserItem extends Authenticatable
{
    public function Item(){
        return $this->hasMany('App/Item');
    }

Code in my controller

public function profile(){
    $inventory_items = Auth::user()->Inventory;
    return view('profile', compact('inventory_items'));
}

So i can get User's items with these code but. When i would like to get item info via this $inventory_items->item its not working. I think this is because of relationship mistakes of mine.

Codes in Profile

@foreach($inventory_items as $inventory_item)

   {{$inventory_item->Item}}<br><br>

@endforeach

Error

(2/2) ErrorException Class 'App/Item' not found (View: C:\xampp\htdocs\X-GOTL\resources\views\profile.blade.php)

Upvotes: 0

Views: 172

Answers (3)

Berke
Berke

Reputation: 760

So guys actually i found the answer i needed. I just needed to create pivot table to make connection between Users and Items. So if someone will have same problem he can watch this video https://www.youtube.com/watch?v=akKWC_vP7sE . Thanks to you for your answers anyways!

Upvotes: 0

ZeroOne
ZeroOne

Reputation: 9117

Dev tend to make mistake \ (correct) and / (wrong).. ErrorException Class 'App/Item' show it cant find 'App/Item' which actually wrong syntax

 return $this->hasMany('App/Item');

change to

 return $this->hasMany(Item::class);

Upvotes: 0

user1965074
user1965074

Reputation: 369

I think that there is a few errors in your code, no? See my inline comments. Also, you seem to be forgetting that there are many inventories per user and many items per inventory so you need a double-foreach loop.

class User extends Authenticatable
{ 
    # There are many inventories per user so put a plural 's' on items
    public function inventories()
    {
        # This should NOT be App\Item

        return $this->hasMany('App\Inventory');
    }
...

# This should NOT be UserItem but Inventory, right? (Remember to rename that file too!)
class Inventory extends Authenticatable
{
    # There are many items per inventory so put a plural 's' on items
    public function items()
    {
        return $this->hasMany('App\Item');
    }

Also your error tells you that you have not created a file App\Item so run php artisan make:model Item.

then in your controller do:

public function profile()
{
        $inventories = Auth::user()->inventories;
        return view('profile', compact('inventories'));
}

and in your view

@foreach($inventories as $inventory)
    @foreach($inventory->items as $item)
        {{ $item->name }} or {{ $item->id }}
    @endforeach
@endforeach

Upvotes: 0

Related Questions