属性 [id] 在这个集合实例上不存在?
Property [id] does not exist on this collection instance?
我想通过UserController显示User使用了多少产品和用户信息。我为它创建了两个 Resources
。所以我使用 $id
搜索他 这是我的 UserController
代码
public function show($id)
{
//
$user = User::find($id);
if (is_null($user)) {
return response()->json([
'message' => 'User Not Found',
'status' => 404
], 404);
}
$products = Product::where('user_id', $user->id)->get();
return response()->json([
'data' => new UserProductResource($products),
'status' => 200
], 200);
}
这是我的 UserProductResource
<?php
namespace App\Http\Resources;
use App\Http\Resources\ProductResource;
use Illuminate\Http\Resources\Json\JsonResource;
class UserProductResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'profile_img' => $this->profile_img,
'products' => ProductResource::make($this->products),
];
}
}
错误是
我的路线是:
Route::get('/show-product/{id}', [UserController::class, 'showProduct']);
试试这个解决方案:
您的查询有误
$products = Product::where('user_id', $user_id)->get();
因为 $user_id 是用户的对象,而不是单个值。
您的代码:
public function show($id)
{
//
$user_id = User::find($id);
if (is_null($user_id)) {
return response()->json([
'message' => 'User Not Found',
'status' => 404
], 404);
}
$products = Product::where('user_id', $user_id)->get();
return response()->json([
'data' => new UserProductResource($products),
'status' => 200
], 200);
}
新代码:
public function show($id)
{
//
$user = User::find($id);
if (is_null($user)) {
return response()->json([
'message' => 'User Not Found',
'status' => 404
], 404);
}
$products = Product::where('user_id', $user->id)->get();
return response()->json([
'data' => new UserProductResource($products),
'status' => 200
], 200);
}
find($id) returns 一个对象,所以你不能在 where() 中传递对象。替换你下面的行
$products = Product::where('user_id', $user_id)->get();
用这条线
$products = Product::where('user_id', $user_id->id)->get();
它会起作用。
new UserProductResource($products)
此处产品对象在资源文件中传递,如果我们检查 UserProductResource 中的字段,它们看起来就像用户模型中存在的字段。
$products 会有类似 products 的字段或关系吗?
$products 是一个集合,因此应使用 UserProductResource::collection()。
理想情况下,您应该在用户和产品之间建立关系并执行此操作
$user = User::with('products')->find($id);
if (is_null($user_id)) {
return response()->json([
'message' => 'User Not Found',
'status' => 404
], 404);
}
return response()->json([
'data' => new UserProductResource($user),
'status' => 200
], 200);
由于 UserProductResource 中可以有多个产品,您可以这样做
'products' => ProductResource::collection($this->products),
我想通过UserController显示User使用了多少产品和用户信息。我为它创建了两个 Resources
。所以我使用 $id
搜索他 这是我的 UserController
代码
public function show($id)
{
//
$user = User::find($id);
if (is_null($user)) {
return response()->json([
'message' => 'User Not Found',
'status' => 404
], 404);
}
$products = Product::where('user_id', $user->id)->get();
return response()->json([
'data' => new UserProductResource($products),
'status' => 200
], 200);
}
这是我的 UserProductResource
<?php
namespace App\Http\Resources;
use App\Http\Resources\ProductResource;
use Illuminate\Http\Resources\Json\JsonResource;
class UserProductResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'profile_img' => $this->profile_img,
'products' => ProductResource::make($this->products),
];
}
}
错误是
我的路线是:
Route::get('/show-product/{id}', [UserController::class, 'showProduct']);
试试这个解决方案:
您的查询有误
$products = Product::where('user_id', $user_id)->get();
因为 $user_id 是用户的对象,而不是单个值。
您的代码:
public function show($id)
{
//
$user_id = User::find($id);
if (is_null($user_id)) {
return response()->json([
'message' => 'User Not Found',
'status' => 404
], 404);
}
$products = Product::where('user_id', $user_id)->get();
return response()->json([
'data' => new UserProductResource($products),
'status' => 200
], 200);
}
新代码:
public function show($id)
{
//
$user = User::find($id);
if (is_null($user)) {
return response()->json([
'message' => 'User Not Found',
'status' => 404
], 404);
}
$products = Product::where('user_id', $user->id)->get();
return response()->json([
'data' => new UserProductResource($products),
'status' => 200
], 200);
}
find($id) returns 一个对象,所以你不能在 where() 中传递对象。替换你下面的行
$products = Product::where('user_id', $user_id)->get();
用这条线
$products = Product::where('user_id', $user_id->id)->get();
它会起作用。
new UserProductResource($products)
此处产品对象在资源文件中传递,如果我们检查 UserProductResource 中的字段,它们看起来就像用户模型中存在的字段。
$products 会有类似 products 的字段或关系吗?
$products 是一个集合,因此应使用 UserProductResource::collection()。
理想情况下,您应该在用户和产品之间建立关系并执行此操作
$user = User::with('products')->find($id);
if (is_null($user_id)) {
return response()->json([
'message' => 'User Not Found',
'status' => 404
], 404);
}
return response()->json([
'data' => new UserProductResource($user),
'status' => 200
], 200);
由于 UserProductResource 中可以有多个产品,您可以这样做
'products' => ProductResource::collection($this->products),