Home > Software design >  Load all relation to a single user laravel
Load all relation to a single user laravel

Time:10-03

I like to get everything related to user

$this->storeproducts = User::find($x)->with(['stores.inventories.product'])->first();

But that don't work as user has not stores table i think (assumption). It shows all users relation all 5 register find($x) don't make any difference.

#items: array:5 [▼
    0 => App\Models\User {#1432 ▶}
    1 => App\Models\User {#1433 ▶}
    2 => App\Models\User {#1448 ▶}
    3 => App\Models\User {#1449 ▶}
    4 => App\Models\User {#1450 ▶}
  ]

that i don't want that i want only that user related details(relations)

3 => App\Models\User {#1449 ▼
      #fillable: array:3 [▶]
      #hidden: array:4 [▶]
      #casts: array:1 [▶]
      #appends: array:1 [▶]
      #connection: "mysql"
      #table: "users"
      #primaryKey: "id"
      #keyType: "int"
       incrementing: true
      #with: []
      #withCount: []
       preventsLazyLoading: false
      #perPage: 15
       exists: true
       wasRecentlyCreated: false
      #attributes: array:14 [▶]
      #original: array:14 [▶]
      #changes: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "stores" => Illuminate\Database\Eloquent\Collection {#1453 ▼
          #items: array:1 [▼
            0 => App\Models\Store {#1458 ▼
              #fillable: array:6 [▶]
              #connection: "mysql"
              #table: "stores"
              #primaryKey: "id"
              #keyType: "int"
               incrementing: true
              #with: []
              #withCount: []
               preventsLazyLoading: false
              #perPage: 15
               exists: true
               wasRecentlyCreated: false
              #attributes: array:8 [▶]
              #original: array:8 [▶]
              #changes: []
              #casts: []
              #classCastCache: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: array:1 [▼
                "inventories" => Illuminate\Database\Eloquent\Collection {#1469 ▼
                  #items: array:1 [▼
                    0 => App\Models\Inventory {#1467 ▼
                      #fillable: array:3 [▶]
                      #connection: "mysql"
                      #table: "inventories"
                      #primaryKey: "id"
                      #keyType: "int"
                       incrementing: true
                      #with: []
                      #withCount: []
                       preventsLazyLoading: false
                      #perPage: 15
                       exists: true
                       wasRecentlyCreated: false
                      #attributes: array:6 [▶]
                      #original: array:6 [▶]
                      #changes: []
                      #casts: []
                      #classCastCache: []
                      #dates: []
                      #dateFormat: null
                      #appends: []
                      #dispatchesEvents: []
                      #observables: []
                      #relations: array:1 [▼
                        "product" => App\Models\Product {#1474 ▼
                          #fillable: array:5 [▶]
                          #connection: "mysql"
                          #table: "products"
                          #primaryKey: "id"
                          #keyType: "int"
                           incrementing: true
                          #with: []
                          #withCount: []
                           preventsLazyLoading: false
                          #perPage: 15
                           exists: true
                           wasRecentlyCreated: false
                          #attributes: array:8 [▶]
                          #original: array:8 [▶]
                          #changes: []
                          #casts: []
                          #classCastCache: []
                          #dates: []
                          #dateFormat: null
                          #appends: []
                          #dispatchesEvents: []
                          #observables: []
                          #relations: []
                          #touches: []
                           timestamps: true
                          #hidden: []
                          #visible: []
                          #guarded: array:1 [▶]
                        }
                      ]
                      #touches: []
                       timestamps: true
                      #hidden: []
                      #visible: []
                      #guarded: array:1 [▶]
                    }
                  ]
                }
              ]
              #touches: []
               timestamps: true
              #hidden: []
              #visible: []
              #guarded: array:1 [▶]
            }
          ]
        }
      ]

Relation are on User(model)

public function stores(){
    return $this->hasMany(Store::class);
}

Relation are on Store(model)

public function inventories() {
    return $this->hasMany(inventory::class);
}

Relation are on Inventory(model)

public function product() {
    return $this->belongsTo(Product::class);
}

CodePudding user response:

find($id) is equivalent to where('id', $id)->first() so with find, you already have a collection, not a query builder

change it to

User::where('id', $x)->with(['stores.inventories.product'])->first();
//or
User::with(['stores.inventories.product'])->find($x);

CodePudding user response:

You could try the following code:

$this->storeproducts = User::with(['stores.inventories.product'])->find($x);

If you have any problem, tell me.

  • Related