以下查询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运行时,返回此错误 - {"错误" {"类型":" Symfony的\元器件\调试\异常\ FatalErrorException""消息":"在数组","文件":" C:\ xampp \ htdocs \ mcdigital \ laravel \ vendor \ bllim \ datatables \ src \ Bllim \ Datatables \中调用成员函数getQuery() Datatables.php""线" 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 Datatables
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。请指教。
感谢提前
答案 0 :(得分:1)
你的错误说明了所有。
您应该将$ usuarios 数组转换为Collection :
$usuarios = DB::select( DB::raw($sql), array('idGrupo' => $idGrupo));
$usuarios= collect($usuarios);
GO AHEAD ...... Reference