laravel 5 双重验证和请求
laravel 5 double validation and request
我做了这个验证并且有效:
public function salvar(CreateEquipamento $Vequip, CreateLocalizacao $VLocal)
{
$this->equipamento->create($Vequip->all());
$equipamento = $this->equipamento->create($input);
return redirect()->route('equipamento.index');
}
我还想做一些事情,比如获取最后创建的设备 ID 并将其包含在数组中以进行验证并为本地验证创建 (CreateLocalizacao $VLocal
),因为我有两个表,一个用于设备另一个存储我的设备所在的所有位置。
$input['equipamento_id'] = $equipamento->id;
$this->localizacao->create($VLocal->all());
我怎么能做这样的事??提前谢谢!
我做了一个 "workarround" 解决方案 ;)
$localizacao = [
'equipamento_id' => $id,
'centrocusto_id' => $input['centrocusto_id'],
'projeto' => $input['projeto'],
'data_movimentacao' => $input['data_movimentacao']
];
$this->localizacao->create($VLocal->all($localizacao));
我不知道这是否是最好的方法,但是可以,但是如果有人有正确的方法,请 post!
您在使用 Laravel 5 吗?
如果是,请使用表单请求,它们使一切变得更容易。如果你需要从一个表单中验证两个东西,你只需在控制器方法中放置两个请求。我在为电子商务页面注册用户时使用它。我需要验证用户数据和地址数据,如下所示:
public function store(UserRegisterRequest $user_request, AddressCreateRequest $add_request)
{
//if this is being executed, the input passed the validation tests...
$user = User::create(
//... some user input...
));
Address::create(array_merge(
$add_request->all(),
['user_id' => $user->id]
));
}}
使用 artisan 创建请求:php artisan make:request SomethingRequest
,它生成一个空请求(注意授权函数总是 returns false
,将其更改为 true 或验证用户的代码有权提出该请求)。
这是一个请求示例:
class AddressCreateRequest extends Request {
public function authorize()
{
return true;
}
public function rules()
{
return [
"fullname" => "required",
//other rules
];
}
}
有关文档的更多信息:
http://laravel.com/docs/5.0/validation#form-request-validation
我做了这个验证并且有效:
public function salvar(CreateEquipamento $Vequip, CreateLocalizacao $VLocal)
{
$this->equipamento->create($Vequip->all());
$equipamento = $this->equipamento->create($input);
return redirect()->route('equipamento.index');
}
我还想做一些事情,比如获取最后创建的设备 ID 并将其包含在数组中以进行验证并为本地验证创建 (CreateLocalizacao $VLocal
),因为我有两个表,一个用于设备另一个存储我的设备所在的所有位置。
$input['equipamento_id'] = $equipamento->id;
$this->localizacao->create($VLocal->all());
我怎么能做这样的事??提前谢谢!
我做了一个 "workarround" 解决方案 ;)
$localizacao = [
'equipamento_id' => $id,
'centrocusto_id' => $input['centrocusto_id'],
'projeto' => $input['projeto'],
'data_movimentacao' => $input['data_movimentacao']
];
$this->localizacao->create($VLocal->all($localizacao));
我不知道这是否是最好的方法,但是可以,但是如果有人有正确的方法,请 post!
您在使用 Laravel 5 吗?
如果是,请使用表单请求,它们使一切变得更容易。如果你需要从一个表单中验证两个东西,你只需在控制器方法中放置两个请求。我在为电子商务页面注册用户时使用它。我需要验证用户数据和地址数据,如下所示:
public function store(UserRegisterRequest $user_request, AddressCreateRequest $add_request)
{
//if this is being executed, the input passed the validation tests...
$user = User::create(
//... some user input...
));
Address::create(array_merge(
$add_request->all(),
['user_id' => $user->id]
));
}}
使用 artisan 创建请求:php artisan make:request SomethingRequest
,它生成一个空请求(注意授权函数总是 returns false
,将其更改为 true 或验证用户的代码有权提出该请求)。
这是一个请求示例:
class AddressCreateRequest extends Request {
public function authorize()
{
return true;
}
public function rules()
{
return [
"fullname" => "required",
//other rules
];
}
}
有关文档的更多信息: http://laravel.com/docs/5.0/validation#form-request-validation