Query in controller from ID with jquery

1

I am trying to extract data from a table from an ID and display it with a datatable.

By jquery, I have the following 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 have defined the following index:

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

But it returns a json empty and incorrect.

In my model, I have defined my Query function:

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

I receive the following request and I need the first value (1) to be able to use it in my query and get the correct data:

Where do I have the error?

Thank you.

    
asked by Sergio Ramirez Dominguez 11.06.2017 в 23:50
source

1 answer

1

I will rethink your Laravel code a little, to take advantage of the Framework's functionalities.

I will assume that in your HistoricoEquipos model the table that is connected is historico_equipos .

The first thing is to eliminate the method Consulta() of the model, which is not needed according to the common practices of Laravel.

So the table in the HistoricoEquipos model must be defined like this:

<?php

namespace app\Models;

use Illuminate\Database\Eloquent\Model;

class Car extends Model
{

    protected $table = 'historico_equipos';

    protected $fillable = [
        // ...
    ];

    // ...

}

Now, in the controller we are going to take advantage of Eloquent to make the query:

public function index(Request $request, $id)
{
    if ($request->ajax()) {
        $historico = HistoricoEquipos::where('id_equipo', $id)
            ->get();

        return response()->json($historico);
    }
    return view('historico.index');
}

You can read more about Eloquent in the documentation: link

After the discussion in the chat, we found that there was an error in the definition of the routes, and that the index method should not be included in Route::resource()

Route::resource('historico','HistoricoEquiposController', ['except' => ['index']]);

The route had to be added separately:

Route::get('historico/{historico}', 'HistoricoEquiposController@index')->name('historico.index');
    
answered by 13.06.2017 / 01:42
source