What could be causing this issue on the server when it works fine locally? Are there any specific server configurations or dependencies that I might be missing? Any suggestions for troubleshooting this problem would be greatly appreciated.
Thank you in advance for your help!
DataTables warning: table id=userprofiles-table – Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
What I’ve Tried:
-
Checked the server configurations to ensure they match the local environment.
-
Verified that all necessary extensions are installed on the server.
-
Cleared the application cache and config cache.
-
Ensured that the database connection settings are correct and the database is up to date.
<?php
namespace AppDataTables;
use AppModelsUser;
use YajraDataTablesHtmlButton;
use YajraDataTablesHtmlColumn;
use YajraDataTablesServicesDataTable;
class UsersDataTable extends DataTable
{
/**
* Build DataTable class.
*
* @param mixed $query Results from query() method.
* @return YajraDataTablesDataTableAbstract
*/
public function dataTable($query)
{
return datatables()
->eloquent($query)
->editColumn('userProfile.country', function($query) {
return $query->userProfile->country ?? '-';
})
->editColumn('userProfile.company_name', function($query) {
return $query->userProfile->company_name ?? '-';
})
->editColumn('status', function($query) {
$status = 'warning';
switch ($query->status) {
case 'active':
$status = 'primary';
break;
case 'inactive':
$status = 'danger';
break;
case 'banned':
$status = 'dark';
break;
}
return '<span class="text-capitalize badge bg-'.$status.'">'.$query->status.'</span>';
})
->editColumn('created_at', function($query) {
return date('Y/m/d',strtotime($query->created_at));
})
->filterColumn('full_name', function($query, $keyword) {
$sql = "CONCAT(users.first_name,' ',users.last_name) like ?";
return $query->whereRaw($sql, ["%{$keyword}%"]);
})
->filterColumn('userProfile.company_name', function($query, $keyword) {
return $query->orWhereHas('userProfile', function($q) use($keyword) {
$q->where('company_name', 'like', "%{$keyword}%");
});
})
->filterColumn('userProfile.country', function($query, $keyword) {
return $query->orWhereHas('userProfile', function($q) use($keyword) {
$q->where('country', 'like', "%{$keyword}%");
});
})
->addColumn('action', 'users.action')
->rawColumns(['action','status']);
}
/**
* Get query source of dataTable.
*
* @param AppModelsUser $model
* @return IlluminateDatabaseEloquentBuilder
*/
public function query()
{
$model = User::query()->with('userProfile');
return $this->applyScopes($model);
}
/**
* Optional method if you want to use html builder.
*
* @return YajraDataTablesHtmlBuilder
*/
public function html()
{
return $this->builder()
->setTableId('dataTable')
->columns($this->getColumns())
->minifiedAjax()
->dom('<"row align-items-center"<"col-md-2" l><"col-md-6" B><"col-md-4"f>><"table-responsive my-3" rt><"row align-items-center" <"col-md-6" i><"col-md-6" p>><"clear">')
->parameters([
"processing" => true,
"autoWidth" => false,
]);
}
/**
* Get columns.
*
* @return array
*/
protected function getColumns()
{
return [
['data' => 'id', 'name' => 'id', 'title' => 'id'],
['data' => 'full_name', 'name' => 'full_name', 'title' => 'FULL NAME', 'orderable' => false],
['data' => 'phone_number', 'name' => 'phone_number', 'title' => 'Phone Number'],
['data' => 'email', 'name' => 'email', 'title' => 'Email'],
['data' => 'userProfile.country', 'name' => 'userProfile.country', 'title' => 'Country'],
['data' => 'status', 'name' => 'status', 'title' => 'Status'],
['data' => 'userProfile.company_name', 'name' => 'userProfile.company_name', 'title' => 'Company'],
['data' => 'created_at', 'name' => 'created_at', 'title' => 'Join Date'],
Column::computed('action')
->exportable(false)
->printable(false)
->searchable(false)
->width(60)
->addClass('text-center hide-search'),
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'Users_' . date('YmdHis');
}
}
#################Controller Method ##############################
public function index(UsersDataTable $dataTable)
{
$pageTitle = trans('global-message.list_form_title', ['form' => trans('users.title')]);
$auth_user = AuthHelper::authSession();
$assets = ['data-table'];
$headerAction = '<a href="' . route('users.create') . '" class="btn btn-sm btn-primary" role="button">Add User</a>';
return $dataTable->render('global.datatable', compact('pageTitle', 'auth_user', 'assets', 'headerAction'));
}
#############Blade########
@push('scripts')
{{ $dataTable->scripts() }}
@endpush
<x-app-layout :assets="$assets ?? []">
<div>
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-header d-flex justify-content-between">
<div class="header-title">
<h4 class="card-title">{{ $pageTitle ?? 'List'}}</h4>
</div>
<div class="card-action">
{!! $headerAction ?? '' !!}
</div>
</div>
<div class="card-body px-0">
<div class="table-responsive">
{{ $dataTable->table(['class' => 'table text-center table-striped w-100'],true) }}
</div>
</div>
</div>
</div>
</div>
</div>
</x-app-layout>