Go through and save data from an array with laravel 5.1?

1

I have the following case, I am sending from the view to the controller some data in the following way:

<div id="modalities_1">
    <div class="col-md-12">
        <h3>Modalidad</h3>
    </div>


    <div class="form-group col-xs-4">
        {!!Form::label('Titulo de modalidad','Titulo de modalidad:')!!}
        {!!Form::text('modalities_1[title_modal]',null,['class'=>'form-control', 'placeholder'=>'Ingrese el titulo de la modalidad', 'id'=>'modalities_1_title_modal1'])!!}
    </div>



    <div class="form-group col-xs-4">
        {!!Form::label('Ingrese el precio para esta modalidad','Ingrese el precio para esta modalidad:')!!}
        {!!Form::text('modalities_1[price]',null,['class'=>'form-control', 'placeholder'=>'Ingrese el precio para esta modalidad','id'=>'modalities_1_price1'])!!}
    </div>


    <div class="form-group col-xs-4">
        {!!Form::label('Cantidad','Cantidad de entradas disponibles:')!!}
        {!!Form::text('modalities_1[quantity]',null,['class'=>'form-control', 'placeholder'=>'Cantidad de entradas disponibles','id'=>'modalities_1_quantity1'])!!}
    </div>

    <div class="form-group col-xs-12">
        {!!Form::label('Ingrese las condiciones','Ingrese las condiciones pra esta modalidad:')!!}
        {!!Form::text('modalities_1[terms]',null,['class'=>'form-control', 'placeholder'=>'Ingrese las condiciones para esta modalidad','id'=>'modalities_1_terms1'])!!}
    </div>

The controller receives it in an array in this way:

This is because it is a dynamic form in which I change the name of the array and I increase it in 1 at the end, that is, modalities_1, modalities_2 and so, the thing is that since it is dynamic I can delete some of them and there are modalities_1 , modalities_3, and so ... does not follow a sequence as such. now this part of the form is associated with a higher part, I must save this form related to another table ... the controller has it this way:

          $modalitys = $request->all();


       // dd($modal);
    $event = new Event;
    $event->name_eve = $request->name_eve;
    $event->description = $request->description;
    $event->slug = $request->slug;
    $event->img = $request->img;
    $event->date_event = $request->date_event;
    $event->date_start = $request->date_start;
    $event->date_end = $request->date_end;
    $event->site = $request->site;
    $event->iva = $request->iva;
    $event->category_id = $request->category_id;
    $event->save();

    $id= $event['id'];
 foreach ($modalitys as $key => $value) {

                $title_modal= $modalitys['modalities_1']['title_modal'];
                $price= $modalitys['modalities_1']['price'];
                $quantity= $modalitys['modalities_1']['quantity'];
                $terms= $modalitys['modalities_1']['terms'];
                $modal = [
                            'title_modal'=>$title_modal,            
                            'price'=>$price,
                            'quantity'=>$quantity,
                            'terms'=> $terms,
                            'events_id'=>$id,
                ];


      }
      Modality::create($modal);

The names of the fields I generate with jquery, are the product of a cloning, so what I do is modify them with some variables, the code, like this:

$(document).ready(function() {

	$(".add-modal").each(function (el){
		$(this).bind("click",addModal);
	});
});

function addModal(){



	var clickID = parseInt($(this).parent('div').parent('div').attr('id').replace('modalities_',''));

var newID = (clickID+1);


var OldId = 'modalities_'+clickID;
$newModal = $('#modalities_'+clickID).clone(true);

$newModal.attr("id",'modalities_'+newID);
var newAttr = $newModal.attr("id");

$newModal.children("div").children("input").eq(0).attr("id",newAttr+'_'+'title_modal'+newID).val('');
$newModal.children("div").children("input").eq(1).attr("id",newAttr+'_'+'price'+newID).val('');
$newModal.children("div").children("input").eq(2).attr("id",newAttr+'_'+'quantity'+newID).val('');
$newModal.children("div").children("input").eq(3).attr("id",newAttr+'_'+'terms'+newID).val('');
$newModal.children("div").children("input").eq(4).attr("id",newID);
$newDiscount = $newModal.children("div").eq(5).attr("id",newAttr+'_'+'discount_1');
$newDiscount.children("div").children("input").eq(0).attr("id",newAttr+'_'+'discount_1_'+'title_dis1').val('');
$newDiscount.children("div").children("input").eq(1).attr("id",newAttr+'_'+'discount_1_'+'formula1').val('');
$newDiscount.children("div").children("input").eq(2).attr("id",newAttr+'_'+'discount_1_'+'value1').val('');
$newDiscount.children("div").children("a").eq(0).attr("id",newAttr+'_'+'discount_1_'+'add-discount1').val('');

$newModal.children("div").children("input").eq(0).attr("name",newAttr+'[title_modal]');
$newModal.children("div").children("input").eq(1).attr("name",newAttr+'[price]');
$newModal.children("div").children("input").eq(2).attr("name",newAttr+'[quantity]');
$newModal.children("div").children("input").eq(3).attr("name",newAttr+'[terms]');


$newDiscount.children("div").children("input").eq(0).attr("name",newAttr+'[discount_1]'+'[title_dis]');
$newDiscount.children("div").children("input").eq(1).attr("name",newAttr+'[discount_1]'+'[formula]');
$newDiscount.children("div").children("input").eq(2).attr("name",newAttr+'[discount_1]'+'[value]');

var dataId= $newDiscount.children("div").children("a").attr("data_id");


if (dataId == 1) {
	$newDiscount.children("div").children("a").eq(0).off("click",delRowDiscount);
	$newDiscount.children("div").children("a").eq(0).on("click",addDiscount);
}

$newDiscount.children("div").children("a").eq(0).html('<i class="fa fa-plus-square"></i> Agregar otro descuento');

$newModal.insertAfter($('#modalities_'+clickID));
$('#'+newAttr).children("div[id*="+OldId+"]").remove();

$("#"+clickID).val('- eliminar modalidad').unbind("click",addModal);

$("#"+clickID).bind("click",delRowModal);
}

function delRowModal() {

$(this).parent('div').parent('div').remove();

}

Which works well with a single form modalities_1 because I specify it in the foreach at the time of traversing, the question would be, observing the image that I uploaded, there are several madalities_x which I must save each of its contents in a table , how could you check these arrays and run them dynamically without taking into account the correlative if not that if there is a key modalities_x I take the route and save it and continue until they do not exist anymore?

    
asked by Susje 29.09.2017 в 16:34
source

1 answer

1

First I try to find out how many modalities are independent of their numerical identifier, after that I go through them each with a for accessing each one and saving their information in the database:

$modalities = array();

foreach ($modalitys as $key => $value) {

    $key_array = explode("_", $key);

    if($key_array[0] == 'modalities'){
        array_push($modalities, $key);
    }
}

for($i = 0; $i < count($modalities); $i++){
    $title_modal = $modalitys[$modalities[$i]]['title_modal'];
    $price= $modalitys[$modalities[$i]]['price'];
    $quantity= $modalitys[$modalities[$i]]['quantity'];
    $terms= $modalitys[$modalities[$i]]['terms'];
    $modal = [
        'title_modal'=>$title_modal,            
        'price'=>$price,
        'quantity'=>$quantity,
        'terms'=> $terms,
        'events_id'=>$id,
    ];

    Modality::create($modal);
}

EXPLANATION REQUESTED:

When you make a foreach to $modalitys in the variable $key all the properties of the object (_token, name_eve, modalities_1, etc, etc) are saved, then with the explode() I am converting the string of $key in a array , leaving something like that [0=>modalities, 1=>1] where index 0 is being the name as such and index 1 has been the numerical identifier that differentiates it from the others, then I say that when the index 0 (bone the name) is equal to modalities is because it is a array that must be traversed and if the condition is fulfilled, I add to the variable $modalities each one of the numerical identifiers that correspond leaving something like this $modalities = array(1, 2, 3.....n);

    
answered by 29.09.2017 / 16:46
source