我在数组中有两个请求参数:
{
"users": [1,2,3],
"groups": [1,2]
}
我想获取ID为1,2,4的用户记录和ID为1,2的组,我可以获取用户记录,但无法获取组记录。
这是我的代码:
public function isGroupUser(Request $request)
{
$usersDetail = $request->input('users', []);
$groupsDetails = $request->input('groups', []);
if($usersDetail)
{
$users = DB::table('users')
->join('user_basic_info','users.id','=','user_basic_info.user_id')
->select('users.id','user_basic_info.first_name')
->whereIn('users.id',$usersDetail)->get();
$resultArray = ['users' => $users];
return \Illuminate\Support\Facades\Response::json($resultArray, 200);
}
elseif ($groupsDetails)
{
$groups = DB::table('group')
->select('group.id','group.name')
->whereIn('group.id',$groupsDetails)->get();
$resultArray = ['groups' => $groups];
return \Illuminate\Support\Facades\Response::json($resultArray, 200);
}
}
我可以获取用户的记录,但不能获取组 我哪里错了? 非常感谢您的帮助!
当前我的json响应:
{
"users": [
{
"id": 1,
"first_name": "Admin"
},
{
"id": 2,
"first_name": "Admin"
},
{
"id": 3,
"first_name": "Admin"
}
]
}
但没有得到分组。 您的帮助需要在这里。
答案 0 :(得分:1)
您遇到了一些问题
class A101Dataset(Dataset):
def __init__(self, json_file, images_dir, transform=None):
self.fb_model = gensim.models.KeyedVectors.load_word2vec_format('./cc.tr.300.vec', encoding='utf-8')
self.data = []
with open(json_file, encoding='utf-8') as f:
raw = json.load(f)
for item in raw:
for img in item['images']:
img_path = f'./Dataset/a101/images/{img}'
self.data.append((img_path, item['product_name']))
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
img_path, product_name = self.data[idx]
words = product_name.split()
sent_vec = np.mean([self.fb_model[w] for w in words], axis=0)
print(sent_vec.shape)
# image = io.imread(img_path)
image = cv2.imread(img_path)
# image = Image.open(img_path)
sample = {'image': image, 'sent_vec': sent_vec}
if self.transform:
sample['image'] = self.transform(sample['image'])
return sample
dataset = A101Dataset(json_file='./Dataset/a101/a101.json', images_dir='./Dataset/a101/images/')
时要尝试为其分配组,更改这两行
resultArray
$resultArray['users'] = $users; // here result array will have key users
$resultArray['groups'] = $groups; // here result array will have key groups
替换为elseif ($groupsDetails)
在上述情况下,它将数据推入数组。您的情况是它的替换数组。
应该可以。
答案 1 :(得分:1)
按如下所示更改代码,
在您的代码中,只有一部分(if
或elseif
)有效
$usersDetail = $request->input('users', []);
$groupsDetails = $request->input('groups', []);
$resultArray = []; // an empty array to store your results
if($usersDetail)
{
$users = DB::table('users')
->join('user_basic_info','users.id','=','user_basic_info.user_id')
->select('users.id','user_basic_info.first_name')
->whereIn('users.id',$usersDetail)->get();
$resultArray['users'] = $users;
}
if ($groupsDetails)
{
$groups = DB::table('group')
->select('group.id','group.name')
->whereIn('group.id',$groupsDetails)->get();
$resultArray['groups'] = $groups;
}
return \Illuminate\Support\Facades\Response::json($resultArray, 200);
答案 2 :(得分:0)
只需删除第一个退货即可。因为如果请求中有用户,那么什么时候该返回,然后从anad中退出,而不是第二次返回。
编辑:@Shobi回答很好