【问题标题】:Laravel 4 - Datatables - Call to a member function getQuery() on arrayLaravel 4 - 数据表 - 调用数组上的成员函数 getQuery()
【发布时间】:2015-02-19 13:18:55
【问题描述】:

以下查询 MySQL 在 MySQL MyAdmin 中正常工作。

SELECT contacts.id, contacts.gsm, contacts.name, contacts.last_name, contacts.email, contacts.created_at,  if(temp.Count >= 1,temp.Count,0) as newCount
        FROM contacts
        LEFT JOIN
        (SELECT contacts.id, COUNT(groupcontact.id) AS Count
        FROM contacts
        LEFT JOIN groupcontact ON groupcontact.contacts_id = contacts.id
        LEFT JOIN groups ON groups.id = groupcontact.groups_id
        WHERE groups.id = :idGrupo
        GROUP BY contacts.id)
        temp ON temp.id = contacts.id

但是当我尝试在 RAW DB 中使用 LARAVEL + Datatables 运行时,返回此错误 - {"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","message":"调用数组上的成员函数 getQuery()","file":"C:\xampp\htdocs \mcdigital\laravel\vendor\bllim\datatables\src\Bllim\Datatables\Datatables.php","line":256}}

不知道为什么


LARAVEL 控制器

$sql="SELECT contacts.id, contacts.gsm, contacts.name, contacts.last_name, contacts.email, contacts.created_at,  if(temp.Count >= 1,temp.Count,0) as newCount
            FROM contacts
            LEFT JOIN
            (SELECT contacts.id, COUNT(groupcontact.id) AS Count
            FROM contacts
            LEFT JOIN groupcontact ON groupcontact.contacts_id = contacts.id
            LEFT JOIN groups ON groups.id = groupcontact.groups_id
            WHERE groups.id = :idGrupo
            GROUP BY contacts.id)
            temp ON temp.id = contacts.id";
    $usuarios = DB::select( DB::raw($sql), array('idGrupo' => $idGrupo));

    return Datatables::of($usuarios)
    ->add_column('options', '
                    <label class="toggle yesNoGroup pull-left">
                      <input type="checkbox" name="checkbox-toggle" id="yesNoGroupchk-{{ $id }}" @if(newCount >0)checked@endif>
                      <i></i></label>
                    ')->make(); 

JS 数据表

if ($(tablaName).length && !isNaN(idGroup)) {
        var $oTable = $(tablaName).dataTable({
            "bServerSide": true,
            "sAjaxSource": '/contact-list-group-load/'+idGroup,
            "autoWidth": true,
            "oLanguage": {
                "sProcessing":     '<image src="/images/ajax-loader.gif" alt="Cargando...">',
                "sLengthMenu":     "Mostrar _MENU_ contactos",
                "sSearch":         "Buscar:",
                "sZeroRecords":    "No se encontraron resultados",
                "sInfo":           "Mostrando _START_ al _END_ de un total de _TOTAL_ contactos",
                "oPaginate": {
                    "sFirst":    "Primero",
                    "sLast":     "Último",
                    "sNext":     "Siguiente",
                    "sPrevious": "Anterior"
                }
            },
            "aaSorting": [[0, 'desc']],
            "sDom":
                    "<'row'<'col-xs-6'l><'col-xs-6'f>r>"+
                    "t"+
                    "<'row'<'col-xs-12 text-center tableCuantosRegistros'i><'col-xs-12 text-center'p>>",
            "sLoadingRecords": '<image src="/images/ajax-loader.gif" alt="Cargando...">',
            "aoColumnDefs": [
                { "bSearchable": false, "aTargets": [ 5, 6 ] },
                { "bSortable": false, "aTargets": [ 5, 6 ] }
              ],
            "fnDrawCallback": function (oSettings) {

           },
            "iDisplayLength": 100
     });

也许,只是想问一下如何将这个 mysql 查询转换为 Laravel Eloquent 或 Laravel Query Builder。请指教。

提前致谢

【问题讨论】:

  • 这是怎么发生的,你在做什么?如果您进一步查看错误堆栈,您可能会发现您自己的一些代码有错误。更多细节会很好。
  • 您应该提供更多信息,以便这里的人可以帮助您,请参考How to ask a good question
  • 谢谢,我添加了更多代码信息
  • 你的 php 版本是什么?一个原因可能是 php 旧版本有时会发生错误,例如“调用成员函数 getQuery()”,特别是在 debian 5.4 中。其他一切正常吗?希望升级php版本有帮助。
  • 嗨!我正在使用 Xampp 和 PHP 版本 5.6.3 工作(本地主机)。谢谢!

标签: php laravel datatable


【解决方案1】:

你的错误说明了一切。

您应该将 $usuarios array 转换为 Collection 为:

$usuarios = DB::select( DB::raw($sql), array('idGrupo' => $idGrupo));
    $usuarios= collect($usuarios);

继续... Reference

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 2017-05-22
    • 2018-06-28
    • 2014-09-15
    • 2019-01-22
    • 2019-01-18
    • 2021-06-15
    • 2016-09-17
    相关资源
    最近更新 更多