mardi 18 février 2020

Problem with edit function using a modal Laravel

I have the next function

public function edit($id)
{
    if(request()->ajax())
    {
        $data = Tbl_Perimetro::findOrFail($id);
        return response()->json(['result' => $data]);

    }
}


public function update(Request $request, Tbl_Perimetro $user)
{
    $rules = array(
        'rif'      =>  'required',
        'razon_social'  =>  'required',
        'holdings_id' => 'required',
        'pines_id' => 'required'

    );


    $error = Validator::make($request->all(), $rules);

    if($error->fails())
    {
        return response()->json(['errors' => $error->errors()->all()]);
    }

    $form_data = array(
        'rif'        =>  $request->rif,
        'razon_social'    =>  $request->razon_social,
        'holdings_id' => $request->holdings_id,
        'pines_id' => $request->pines_id

    );

    Tbl_Perimetro::whereId($request->hidden_id)->update($form_data);

    return response()->json(['success' => 'Datos actualizados satisfactoriamente.']);

}

My problem is that I would like to work with some models... and I know how to work with one model which is Tbl_Perimetro... I would like to work with another model called Tbl_Holding, But I'm using a modal to edit all this information.. here is the code of the modal:

$('#create_record').click(function(){
$('.modal-title').text('Add New Record');
$('#action_button').val('Add');
$('#action').val('Add');
$('#form_result').html('');

  $('#formModal').modal('show');
 });

$('#user_form').on('submit', function(event){
event.preventDefault();
var action_url = '';

if($('#action').val() == 'Add')
{
action_url = "";
} 
if($('#action').val() == 'Editar')
{
action_url = "";
}
  $.ajax({
url: action_url,
method:"POST",
data:$(this).serialize(),
dataType:"json",
success:function(data)
{
var html = '';
if(data.errors)
{
 html = '<div class="alert alert-danger">';
 for(var count = 0; count < data.errors.length; count++)
 {
  html += '<p>' + data.errors[count] + '</p>';
 }
 html += '</div>';
}
if(data.success)
{
 html = '<div class="alert alert-success">' + data.success + '</div>';
 $('#user_form')[0].reset();
 $('#user_perimetro').DataTable().ajax.reload();
}
$('#form_result').html(html);
}
});
});




 $(document).on('click', '.edit', function(){
var id = $(this).attr('id');
$('#form_result').html('');

$.ajax({

url :"/perimetro/"+id+"/edit",
dataType:"json",
success:function(data)
 {
$('#rif').val(data.result.rif);
$('#razon_social').val(data.result.razon_social);
$('#holdings_id').val(data.result.holdings_id);
$('#pines_id').val(data.result.pines_id);
$('#carteras_id').val(data.result.carteras_id);
$('#hidden_id').val(id);
$('.modal-title').text('Editar Registro');
$('#action_button').val('Editar');
$('#action').val('Editar');
$('#formModal').modal('show');
}
})
});

This is the button to edit the information

$button = '<button type="button" name="edit" id="'.$data->id.'" class="edit btn btn-primary btn-sm">Editar</button>';

I would like to know is it exist a way to work with two models in the same modal... In this way I only can work with one model at the same time...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire