Save inputs with arrays with PHP / Laravel

1

I have a dynamically generated table where I enter multiple products. I'm trying to use a for to save these products, but if I have only one product that does not work, it needs to have more than one. I tried with foreach but it did not work.

View:

@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
    <div class="col-md-12">
        <form method="post" action="{{ route('storeProduct') }}">
            {{ csrf_field() }}
            <div class="panel panel-default">
                <div class="panel-heading">Dashboard</div>
                <div class="panel-body">
                    <table class="table m-0" id="products-table">
                        <thead>
                            <tr>
                                <th>Produto/Serviço</th>
                                <th>Quantidade</th>
                                <th>Valor Unitário</th>
                                <th>Valor Total</th>
                                <th>Remover</th>
                            </tr>
                        </thead>
                        <tbody class="row">

                        </tbody>
                        <tfoot>
                            <tr>
                                <td colspan="5" style="text-align: left;">
                                    <button class="btn btn-info waves-effect w-md waves-light m-b-5" onclick="AddTableRow()" type="button">Adicionar Produto</button>
                                </td>
                            </tr>
                        </tfoot>
                    </table>    
                    <button class="btn btn-primary" type="submit">Incluir Orçamento</button>
                </div>
            </div>
        </form>
    </div>
</div>
</div>
@endsection

@push('js')
<script>

RemoveTableRow = function(handler) {
    var tr = $(handler).closest('tr');
    tr.fadeOut(400, function() {
        tr.remove();
    });
    return false;
};

AddTableRow = function() {

    var newRow = $("<tr>");
    var cols = "";

    cols += '<td class="col-md-4"><input type="text" class="form-control product" name="name[]"></td>';
    cols += '<td class="col-md-2"><input type="text" class="form-control amount" name="amount[]"></td>';
    cols += '<td class="col-md-2"><input type="text" class="form-control price" name="unit_price[]"></td>';
    cols += '<td class="col-md-2 total"><input readonly type="text" class="form-control" name="total_price[]"></td>';
    cols += '<td class="col-md-2">';
    cols += '<button class="btn btn-danger waves-effect w-md waves-light m-b-5" onclick="RemoveTableRow(this)" type="button">Remover</button>';
    cols += '</td>';

    newRow.append(cols);
    $("#products-table").append(newRow);

    $(".amount, .price").unbind('blur keyup');
    $(".amount, .price").on("blur keyup",function(){
        const tr = $(this).parent().parent();

        const quant = parseInt(tr.find('.amount').val());
        const valor = parseInt(tr.find('.price').val());
        var total = quant * valor;

        if (!isNaN(quant) && !isNaN(valor)){
            tr.find('.total').html('<input readonly type="text" class="form-control" name="total_price[]" value=" '+total+' ">');
        }
    });

    return false;
};

</script>
@endpush

$ request:

array:5 [▼
   "_token" => "wLRaFmgHxEMtOzPMzzylPFjtJLawgr262kOz4xGf"
   "name" => array:2 [▼
    0 => "Produto 1"
    1 => "Produto 1"
   ]
  "amount" => array:2 [▼
    0 => "2"
    1 => "3"
  ]
  "unit_price" => array:2 [▼
    0 => "200"
    1 => "300"
   ]
   "total_price" => array:2 [▼
   0 => "400"
   1 => "900"
   ]
]

Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;

class ProductsController extends Controller
{

private $product;

public function __construct(Product $product)
{
    $this->product = $product;
}

public function store(Request $request)
{
    for ($i = 0; $i < count($request->all()); $i++) { 
        $this->product = new Product();
        $this->product->name = $request->name[$i];
        $this->product->amount = $request->amount[$i];
        $this->product->unit_price = $request->unit_price[$i];
        $this->product->total_price = $request->total_price[$i];
        $this->product->save();
    }        
}

}
    
asked by anonymous 29.08.2017 / 18:27

1 answer

1

Actually there is a simple way is to use the create method passing array to store the data, and remembering that your Product class should be set to fillable as follows:

use Illuminate\Database\Eloquent\Model;

class Product extends Model 
{
    protected $fillable = ['name','amount','unit_price','total_price'];
}

and in the method:

public function store(Request $request)
{
    for ($i = 0; $i < count($request->name); $i++) 
    { 
         $this->product->create([
                    'name' => $request->name[$i],
                    'amount' => $request->amount[$i] ,
                    'unit_price' => $request->amount[$i],
                    'total_price' => $request->total_price[$i]
         ]);
    } 
}
Note: In your method store had a line: $this->product = new Product(); this can not happen, because if you are recreating an instance that has already been set by constructor . If you want to use that way then use: $product = new Product(); and proceed with those commands.

30.08.2017 / 02:00