send an array from javascript to the laravel driver

-1

I'm doing a query from jquery to a controller in laravel, the problem I have is that I'm sending a total of 5 variables using json , one of them is an array but the problem is that the controller does not want me recognize the variable in the WhereIN and print it to me empty, but when I'm going to place static data, which is serving correctly, I'll leave the following codes.

This is javascript, I want to send the array_granjas for json and when I print it for the console it's fine, it shows me the array but in laravel

 var array_granjas = [];

    $('#select_granjas').on('change', function(e){
        array_granjas.push($(e.currentTarget).val())
        console.log('value_select_prueba: --------->', array_granjas);
    })

    $("#elaborar_grafica_granjas_conversion_final").click(function () {
        var datachart = Array();
        var token = $("#token").val();
        var a_granjas = $("#select_granjas_conversion_final").val();
        var mes = $("#select_granjas_mes").val();

        var json = {
            annio_granjas:a_granjas,
            granjas:array_granjas,
            mes:mes
        }

        console.log(json);

        if (a_granjas == '') {
            swal({
                title:"No hay Fecha seleccionada.",
                text:'',
                type:'warning',
                showCancelButton:false,
                confirmButtonClass:'btn-warning',
                confirmButtonText:'Corregir',
            });
        }else{
            $.ajax({
                method:'POST',
                headers:{'X-CSRF-TOKEN':token},
                url:'http://201.236.212.130:82/intranetcercafe/public/admin/report_precebo_conversion_final_granjas',
                data:json
            }).done(function (msg) {
               console.log(msg); 
            })
        }

here in the laravel driver is where it starts to fail

public function report_precebo_conversion_final_granjas(Request $request){
    $collection = Precebo::join('granjas','granjas.id','=','formulario_precebo.granja_id')
    ->select('granjas.nombre_granja', DB::raw('date_format(str_to_date(mes_traslado,"%m"),"%M"),avg(conversion_ajust_fin) as total'))
    ->where([
        ['mes_traslado',$request->mes],
        ['año_destete',$request->annio_granjas]
        ])
    ->whereIn('granja_id',[$request->granjas])
    ->groupBy(DB::raw('date_format(str_to_date(mes_traslado,"%m"),"%m"),granjas.nombre_granja'))->get();

    $arrayT = [];
    foreach ($collection as $value) {
        $arrayT[] = [$value->nombre_granja,$value->total];
    }
    return response()->json(['status'=>'success','data'=>$arrayT],200);
 }

but here in the whereIn that they see, the array does not arrive and I print empty, but when I put static data, it executes correctly, I need help with that array.

    
asked by Juan Esteban Yarce 07.03.2018 в 19:53
source

1 answer

1

When paracer is only a parameter error in:

  

whereIn('granja_id',[$request->granjas])

Since you send a multidimensional array, and you should only send a one-dimensional array.

Try changing that line by:

whereIn('granja_id',$request->granjas)

    
answered by 07.03.2018 в 22:08