2015-04-06 110 views
0

我這樣做驗證和工作原理:laravel 5雙重驗證,並要求

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()); 

我怎麼會做這樣的事情? thx提前!

我做了一個「workarround」解決方案;)

$localizacao = [ 
       'equipamento_id' => $id, 
       'centrocusto_id' => $input['centrocusto_id'], 
       'projeto' => $input['projeto'], 
       'data_movimentacao' => $input['data_movimentacao'] 
       ]; 

$this->localizacao->create($VLocal->all($localizacao)); 

我不知道這是否是做的最好的方式,但工作原理,但如果有人必須做後,請以正確的方式!

+0

發佈一個答案。如果您需要葡萄牙語的幫助,請將其發佈到葡萄牙語的stackoverflow並鏈接到此處。我能幫你。 (Se precisar de ajuda emportuguês,posta no brasileiro epõeo link aqui)。 – hfingler

回答

1

你在使用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] 
    )); 
}} 

使用工匠製作的要求:php artisan make:request SomethingRequest,它會生成一個空的請求(注意授權函數總是返回false,將其更改爲真或代碼驗證用戶是否有權發出該請求)。

這裏有一個請求的例子:

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