mardi 4 août 2020

Laravel empty array issue

I'm having a problem when I submit form data and display it in another view, I get all the data except an array data type ($mat_res), here is my code:

View :

            $('#rest_form').on('submit', function(event) {
                //event.preventDefault();
                var form_data = $(this).serialize();                    
                var personnel = $('#personnel').val();
                var fonction = $('#fonction').val();
                var service = $('#service').val();
                var user_id = $('#user_id').val();
                var remarque = $('#remarque').val();
                $.ajax({
                    url: "",
                    method: "POST",
                    data: {
                        mat_codes: mat_codes,
                        personnel: personnel,
                        fonction: fonction,
                        remarque: remarque,
                        user_id: user_id
                    },                        
                    success: function(data) {
                        mat_codes = [];
                        $('#rest_form')[0].reset();
                    }
                });                   
            });

Controller :

public function createRest(Request $request) {
        ...
        $mat_res = array();
        for ($i = 0; $i < count($request->mat_codes); $i++) {
                    if(($request->mat_codes)[$i] != "") {
                        Restitution::firstOrCreate(['mat_code' => ($request->mat_codes)[$i], 'personnel_id' => $personnel->id], ['mat_code' => ($request->mat_codes)[$i], 'personnel_id' => $personnel->id, 'num' => $num, 'mois' => date('m'), 'restitution_code' => $num . "/" . date('m') . "/" . date("Y"), 'user_id' => $request->user_id]);
                        Materiel::where('mat_code', '=', ($request->mat_codes)[$i])->update(array('mat_etat' => 2));
                        $mat_res[$i] = DB::table('Materiels')->join('Types', 'Materiels.type_id', '=', 'Types.type_id')->select('Types.type_designation', 'Materiels.mat_designation', 'Materiels.mat_description', 'Materiels.mat_ns', 'Materiels.note')->where('mat_code', ($request->mat_codes)[$i])->first();
                    }
                }
        ...
        return view('Restitution', compact('nom', 'fonction', 'imei1', 'imei2', 'remarque', 'rest_code'), ['mat_res' => $mat_res]);
}

Second view :

@foreach($mat_res as $mat_res)
 ...
    <td></td>
 ...
@endforeach


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire