Save various data in Laravel

1

I have the following formula on my blade, and now I want to save the data for each row in my database. How to save this data in Laravel?

 for ($i = 0; $i < 12; $i++){
        var minhaData = moment(DataVencimento).add($i, 'months');
        ParcelaVencimento =  minhaData.format('DD/MM/YYYY');
        var linha = '<tr class="selected" id="linha'+cont1+'">    <td> <button type="button" class="btn btn-warning" onclick="apagar('+cont1+');"> X </button></td>      <td> <input type="hidden" name="cont1[]" value="'+cont1+'">'+cont1+'</td>  <td> <input type="text" name="ParcelaVencimento[]" value="'+ParcelaVencimento+'"></td> <td> <input type="number" name="ParcelaValor[]" value="'+ParcelaValor+'"></td> </tr>'
        cont1++;
  }

I tried to do this in my Controller to save but this is returning the error        "count (): Parameter must be an array or an object that implements Countable"

public function store(BoletoFormRequest $request){           
        $ParcelaVencimento=$request->get('ParcelaVencimento');
        $ParcelaValor=$request->get('ParcelaValor');

        $cont = 0;
        while($cont < count($QtdParcela)){
        $Boleto = new Boleto();
        $Boleto->ParcelaVencimento=$ParcelaVencimento[$cont];
        $Boleto->ParcelaValor=$ParcelaValor[$cont];
        $Boleto->save();
        $cont=$cont+1;
       }
}

I'm a beginner, what's the best way to save this data?

    
asked by anonymous 29.04.2018 / 22:29

1 answer

0

I do not think there is a better method, but there are some patterns we can follow. I'll give you an example I follow.

Route:

routes / web.php

Route::get('/uri', 'arquivoController@store')->name('nomeDaRota');

Controller:

public function depositStore(MoneyValidationFormRequest $request){

    $ParcelaVencimento = $request->get('ParcelaVencimento');
    $ParcelaValor = $request->get('ParcelaValor');
    $QtdParcela = $request->get('QtdParcela');

    $response = $store->armazenar($ParcelaVencimento, $ParcelaValor, $QtdParcela );

    if ($response['success']){
        return redirect()
                    ->route('rotaHome')
                    ->with('success', $response['message']);
    }

    return redirect()
                ->back()
                ->with('error', $response['message']);

}

Model:

    public function armazenar($parcelaVencimento, $parcelaValor, $qtdParcela){           

        try {

        $cont = 0;
            while($cont < count($QtdParcela)){
              $Boleto = new Boleto();
              $Boleto->ParcelaVencimento=$ParcelaVencimento[$cont];
              $Boleto->ParcelaValor=$ParcelaValor[$cont];
             // DB : Tratamento para commit ou Rollback

              DB::beginTransaction();

              $Boleto->save();

              DB::commit();

              $cont=$cont+1;
           }

           return [
                    'success' => true,
                                    'message' => 'Boletos registrados com sucesso!'
                ];

        }catch(\Exception $e){
            DB::rollBack();
            return [
                'success' => false,
                                'message' => 'Falha ao registrar boleto. ERRO : ' . $e->getMessage()
            ];
        }
    }
}

I believe you can rely on this example applied to your code. I do not quite understand the variable $ qtdparcela, so maybe the code does not work.

    
30.04.2018 / 18:30