Laravel: 如何对单个元素使用验证器?
Laravel: how to use validator for single elements?
我正在尝试在我的函数中验证 $id
和 $offer_id
:
public function getMessagesForOffer($id, $offer_id)
{
$validator = Validator::make($request->all(), [
'id' => 'required|numeric',
'offer_id' => 'required|numeric',
]);
if ($validator->fails()) {
return response([
'status' => 'error',
'error' => 'invalid.credentials',
'message' => 'Invalid credentials'
], 400);
}
}
这是一个错误:"message": "Undefined variable: request",
而且我看到它的编码不正确,当我的函数中没有请求时,我该如何更正它以使用单个元素?
$request->all()
只是 return 数组,所以你也可以在这里使用数组,所以你可以使用:
而不是 $request->all()
['id' => $id, 'offer_id' => $offer_id]
或更短:
compact('id', 'offer_id')
我正在尝试在我的函数中验证 $id
和 $offer_id
:
public function getMessagesForOffer($id, $offer_id)
{
$validator = Validator::make($request->all(), [
'id' => 'required|numeric',
'offer_id' => 'required|numeric',
]);
if ($validator->fails()) {
return response([
'status' => 'error',
'error' => 'invalid.credentials',
'message' => 'Invalid credentials'
], 400);
}
}
这是一个错误:"message": "Undefined variable: request",
而且我看到它的编码不正确,当我的函数中没有请求时,我该如何更正它以使用单个元素?
$request->all()
只是 return 数组,所以你也可以在这里使用数组,所以你可以使用:
$request->all()
['id' => $id, 'offer_id' => $offer_id]
或更短:
compact('id', 'offer_id')