Query in Controller from ID with jQuery

I'm trying to get data form a database table from an ID and show it in a datatable.

With jQuery, I have the next function:

var info = function(tbody, table){
$(tbody).on("click","a[id=ButtonMas]", function(){
        if(table.row(this).child.isShown()){
            var data = table.row(this).data();
        }else{
            var data = table.row($(this).parents("tr")).data();
        }

        var pc = data["id"];
        route = "/historico/"+pc+"";

        $('#ModalInfoEquipos').modal('show');

        var dt_historico = $('#t_historico').DataTable({
            "ajax": {
                "url": route,
                "dataSrc": ""
                },
            "columns": [
                { "data": "created_at" },
                { "data": "estado" },
                { "data": "ubicacion" },
                { "data": "empleado" },
                { "data": "f_asignacion" },                    ],
            "language": {
                    "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Spanish.json"
                },
            "scrollX": true,
        });
});
}

In my Controller, I've implemented the next index:

public function index(Request $request, $id)
{
    if ($request->ajax()) {
        $historico = HistoricoEquipos::Consulta($id);
        return response()->json($historico);
    }
    return view('historico.index');
}

But my response->json get is empty an incorrect.

In my Model, I've implemented my function Consulta:

public static function Consulta($id){
    return DB::table('historico_equipos')
        ->select('historico_equipos.*')
        ->where('id_equipo', $id)
        ->get();
}

I get the next request and need to take the first value(1) for my query:

Request - Chrome Inspector

Where is the error?

Thanks.

Upvotes: 0

Views: 64

Answers (1)

I have an error in my route definition.

I've changed this:

Route::resource('historico','HistoricoEquiposController')

To this:

Route::resource('historico','HistoricoEquiposController', ['except' =>['index']]);
Route::get('historico/{historico}', 'HistoricoEquiposController@index')->name('historico.index');

With help of @Shaz. Thanks.

Upvotes: 1

Related Questions