Call to Undefined method Illuminate Database Query Builder::table()

Asked

Viewed 2,182 times

1

I’m trying to popular a datatable, but I’m not getting any help. Could you help me?

============= Avaliativo_relatorio - Model ========

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Avaliativo_Relatorio extends Model
{
    protected $connection = 'mysql2';
    protected $fillable = [
        'id',
        'firstname',
        'lastname',
        'email',
        'courseid',
        'fullname'
    ];
}

============= Evaluative_reportariocontroller - Controller ========

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Avaliativo_Relatorio;

use Yajra\DataTables\Facades\DataTables;

class Avaliativo_RelatorioController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('relatorios_ava.avaliativo_relatorio');
    }

    public function get_datatable_avaliativo()
    {
        $avaliativo = Avaliativo_Relatorio::table('mdl_role_assignments')
        ->join('mdl_context', 'mdl_role_assignments.contextid', '=', 'mdl_context.id')
        ->join('mdl_course', 'mdl_course.id', '=', 'mdl_context.instanceid')
        ->join('mdl_user', 'mdl_user.id', '=', 'mdl_role_assignments.userid')
        ->where('mdl_context.contextlevel','=',50)
        ->where('mdl_role_assignments.roleid','=',5);
        return Datatables::of($avaliativo)->make(true);
    }
}

============= evaluative_report. ========

ID Name Surname E-mail Id Curso Course

============= master.Lade ========

<script type="text/javascript">
$(document).ready(function() {
     $('#users-table-avaliativo').DataTable({
        "language": {
            "lengthMenu": "Exibindo _MENU_ registros por página",
            "zeroRecords": "Nada encontrado - desculpe",
            "info": "Exibindo página _PAGE_ de _PAGES_",
            "infoEmpty": "Nenhum registro disponível",
            "infoFiltered": "(filtrado de _MAX_ total registros)",
            "sLoadingRecords":  "Carregando Registros...",
            "sProcessing":      "Processando...",
            "sSearch":          "Pesquisar",
            "oPaginate": {
            "sFirst":       "Primeira",
            "sPrevious":    "Anterior",
            "sNext":        "Próxima",
            "sLast":        "Última"
        }
        },
        "processing": true,
        "serverSide": true,
        "ajax": "{{ route('relatorios_ava.avaliativo_relatorio.get_datatable_avaliativo') }}",
        "columns":[
            {"data": "id"},
            {"data": "firstname"},
            {"data": "lastname"},
            {"data": "email"},
            {"data": "courseid"},
            {"data": "fullname"}
        ]
     });
});
</script>

1 answer

1

Instead of Avaliativo_Relatorio, use:

$avaliativo = \DB::table('mdl_role_assignments')...->get();

Or you can add the property $table the class Avaliativo_Relatorio, thus:

protected $table = 'mdl_role_assignments';

And then make use of that same code without the function table, thus:

Avaliativo_Relatorio::join('mdl_context', 'mdl_role_assignments.contextid', '=', 'mdl_context.id')...->get();

This way, you will be able to make the call in the database assuming there is no more error.

  • 1

    i put ->get() at the end of get_datatable_evaluative, is gone the error of "Call to Undefined method Illuminate Database Query Builder::table()". The problem now is that now it is displaying the page but when I update it appears the error in the datatable. " Datatables Warning: table id=users-table-avaliativo - Ajax error. For more information about this error, Please see http://datatables.net/tn/7". I don’t know why he is searching this table users-table evaluative, because I am specifying that my connection is mysql2 in my model which is another external database that I use.

  • This problem is not of connection, it is in html, Javascript is trying to search an element with the id equal to users-table-avaliativo but is not finding, make sure your table is like this <table id="users-table-avaliativo"...

  • Yes, it is very correct, $('#users-table-evaluative'). Datatable(ː which calls <table id="users-table evaluative" class="table">

  • I don’t know, I’m not populating at all. You could help me, it could be that my eloquent consultation is wrong. How would I pass the following query below to ELOQUENT? SELECT u.id, u.firstname,u.lastname,u.email,c.id AS courseid,c.fullname FROM mdl_role_assignments rs INNER JOIN mdl_context and ON rs.contextid=e. id INNER JOIN mdl_course c ON c.id = e.instanceid INNER JOIN mdl_user u ON u.id=rs.userid WHERE e.contextlevel=50 AND rs.roleid=5

  • Put that query in the Eloquent of your question in the function dd(query); and see if there’s any feedback. If not, you use a query pure without Eloquent through the \DB::select(queryPura); and plays the result in dd to see if you’re looking for something.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.