php - 将所有关系加载到单个用户 laravel
问题描述
我喜欢获取与用户相关的所有内容
$this->storeproducts = User::find($x)->with(['stores.inventories.product'])->first();
但这不起作用,因为用户没有存储我认为的表(假设)。它显示所有用户关系所有 5 个寄存器 find($x) 没有任何区别。
#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 ▶}
]
我不希望我只想要与用户相关的详细信息(关系)
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 [▶]
}
]
}
]
关系在用户(模型)上
public function stores(){
return $this->hasMany(Store::class);
}
关系在商店(模型)
public function inventories() {
return $this->hasMany(inventory::class);
}
关系在库存(模型)上
public function product() {
return $this->belongsTo(Product::class);
}
解决方案
find($id)
等价于where('id', $id)->first()
find,你已经有一个集合,而不是一个查询生成器
将其更改为
User::where('id', $x)->with(['stores.inventories.product'])->first();
//or
User::with(['stores.inventories.product'])->find($x);