在 laravel 中使用 pluck() 辅助函数
Using pluck() helper function in laravel
我正在 laravel 5.5
上构建一个小型应用程序,其中我从以下格式的表单中获取包含多个用户信息的列表:
{
"name":"Test",
"description":"Test the description",
"users":[
{
"value":"XYZabc123",
"name":"Nitish Kumar",
"email":"nitishkumar@noeticitservices.com"
},
{
"value":"MFnjMdNz2DIzMJJS",
"name":"Rajesh Kumar Sinha",
"email":"rajesh@noeticitservices.com"
}
]
}
我只想通过 laravel collection
从用户数组中获取 value
键,如下所示:
$userIds = $request->users->pluck('value');
这样我就可以将它们放入查询中:
$user = User::all()->whereIn('unique_id', $userIds);
可能是我做错了大部分事情,但我的主要动机是使用 laravel collection
或 helper
函数并为此编写更清晰的代码:
$teamData['name'] = $request->name;
$teamData['description'] = $request->description;
$teamData['unique_id'] = str_random();
$users = $request->users;
$team = Team::create($teamData);
if($team)
{
$userIds = [];
foreach ($users as $user)
{
$getUser = User::where('unique_id', $user['value'])->get()->first();
$userIds [] = $getUser->id;
}
$team->users()->attach($userIds);
return response()->json(['message' => 'Created Successfully'], 200);
}
return response()->json(['message' => 'Something went wrong'], 500);
我还在学习collections,欢迎任何建议。谢谢
来自 $request
(表单)的数据不是 collection。它是一个数组。如果你需要它是collection,你应该先把它转换成collection。
PS。如果您在单个方法中有多个数据库操作,那么最好有数据库事务。
\DB::transaction(function () use ($request) {
// convert it to collection
$users = collect($request->users);
$team = Team::create([
'name' => $request->name,
'description' => $request->description,
'unique_id' => str_random(),
]);
$team->users()->attach($users->pluck('value')->toArray());
});
// HTTP Created is 201 not 200
return response()->json(['message' => 'Created Successfully'], 201);
或者您需要这样的东西:
return \DB::transaction(function () use ($request) {
$users = collect($request->users);
$team = Team::create([
'name' => $request->name,
'description' => $request->description,
'unique_id' => str_random(),
]);
$team->users()->attach($users->pluck('value')->toArray());
return response()->json([
'message' => 'Created Successfully',
'data' => $team,
], 201);
});
I just want to get the value key form the users array via laravel collection
只需使用 map
然后:
$userIds = $request->users->map(function($user) {
return $user->value; // or $user['value'] ? not sure if this is an array
});
编辑:
如果 $request->users
不是一个集合,在调用 map 之前把它变成一个集合:
$users = collect($request->users);
$userIds = $users->map(function($user) {
return $user->value; // or $user['value'] ? not sure if this is an array
});
我正在 laravel 5.5
上构建一个小型应用程序,其中我从以下格式的表单中获取包含多个用户信息的列表:
{
"name":"Test",
"description":"Test the description",
"users":[
{
"value":"XYZabc123",
"name":"Nitish Kumar",
"email":"nitishkumar@noeticitservices.com"
},
{
"value":"MFnjMdNz2DIzMJJS",
"name":"Rajesh Kumar Sinha",
"email":"rajesh@noeticitservices.com"
}
]
}
我只想通过 laravel collection
从用户数组中获取 value
键,如下所示:
$userIds = $request->users->pluck('value');
这样我就可以将它们放入查询中:
$user = User::all()->whereIn('unique_id', $userIds);
可能是我做错了大部分事情,但我的主要动机是使用 laravel collection
或 helper
函数并为此编写更清晰的代码:
$teamData['name'] = $request->name;
$teamData['description'] = $request->description;
$teamData['unique_id'] = str_random();
$users = $request->users;
$team = Team::create($teamData);
if($team)
{
$userIds = [];
foreach ($users as $user)
{
$getUser = User::where('unique_id', $user['value'])->get()->first();
$userIds [] = $getUser->id;
}
$team->users()->attach($userIds);
return response()->json(['message' => 'Created Successfully'], 200);
}
return response()->json(['message' => 'Something went wrong'], 500);
我还在学习collections,欢迎任何建议。谢谢
来自 $request
(表单)的数据不是 collection。它是一个数组。如果你需要它是collection,你应该先把它转换成collection。
PS。如果您在单个方法中有多个数据库操作,那么最好有数据库事务。
\DB::transaction(function () use ($request) {
// convert it to collection
$users = collect($request->users);
$team = Team::create([
'name' => $request->name,
'description' => $request->description,
'unique_id' => str_random(),
]);
$team->users()->attach($users->pluck('value')->toArray());
});
// HTTP Created is 201 not 200
return response()->json(['message' => 'Created Successfully'], 201);
或者您需要这样的东西:
return \DB::transaction(function () use ($request) {
$users = collect($request->users);
$team = Team::create([
'name' => $request->name,
'description' => $request->description,
'unique_id' => str_random(),
]);
$team->users()->attach($users->pluck('value')->toArray());
return response()->json([
'message' => 'Created Successfully',
'data' => $team,
], 201);
});
I just want to get the value key form the users array via laravel collection
只需使用 map
然后:
$userIds = $request->users->map(function($user) {
return $user->value; // or $user['value'] ? not sure if this is an array
});
编辑:
如果 $request->users
不是一个集合,在调用 map 之前把它变成一个集合:
$users = collect($request->users);
$userIds = $users->map(function($user) {
return $user->value; // or $user['value'] ? not sure if this is an array
});