Save various data in Laravel

Asked

Viewed 737 times

1

I have the following formula in my Blade, and now I want to save the data of 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 getting back 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 am beginner, which is the best method to save this data?

1 answer

0

I believe that the best method does not exist, but there are some patterns that we can follow. I will give an example that 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 didn’t quite understand the variable $qtdparcela, so maybe the code doesn’t work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.