I am getting an error Call to a member function eloquent() on null, when I die dump the $this->query it returns a builder, but the error is on this line:
{message: "Call to a member function eloquent() on null",鈥
exception
:
"Symfony\Component\Debug\Exception\FatalThrowableError"
file
:
"C:\xampp\htdocs\altcoin\app\Tables\Wallet\WalletDatatable.php"
line
:
19
message
:
"Call to a member function eloquent() on null"
trace
:
[{function: "dataTable", class: "App\Tables\Wallet\WalletDatatable", type: "->"},鈥
Line 19 which is "->eloquent($this->query())"
I am using 8.0 and laravel 5.5
<?php
namespace App\Tables\Wallet;
use App\Models\Wallet\Wallet;
use Illuminate\Support\Facades\Auth;
use Yajra\DataTables\Services\DataTable;
class WalletDatatable extends DataTable
{
/**
* Build DataTable class.
*
* @return \Yajra\Datatables\Engines\BaseEngine
*/
public function dataTable()
{
return $this->datatables
->eloquent($this->query())
->addColumn('DT_RowId', function($wallet){
return 'wallet-' . $wallet->id;
})
->addColumn('created_at', function($wallet) {
return $wallet->created_at->diffForHumans();
})
->addColumn('action', function($wallet) {
$objectId = $wallet->id;
$edit = 'wallet.edit';
$stats_path = 'wallet.statistics';
$identifier = 'wallet';
$delete_path = 'wallet.destroy';
$show_path = 'wallet.show';
$dup_path = 'wallet.duplicate';
return view('partials.table-buttons', compact('objectId', 'edit', 'delete_path', 'show_path', 'identifier', 'stats_path', 'dup_path'))
->render();
})
->rawColumns(['name', 'description', 'action']);
}
/**
* Get the query object to be processed by dataTables.
*
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
*/
public function query()
{
$query = Wallet::query()->select($this->getColumns())->where('user_id', Auth::user()->id)
->orderBy('created_at', 'desc');
return $this->applyScopes($query);
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\Datatables\Html\Builder
*/
public function html()
{
return $this->builder()
->columns(['name', 'description', 'created_at'])
->minifiedAjax('')
->addAction(['width' => '80px'])
->parameters([
'dom' => '<"datatable-header"fl><"datatable-scroll"t><"datatable-footer"ip>B',
'order' => [[0, 'desc']],
'buttons' => [
'export',
'print',
'reset',
'reload',
],
]);
}
/**
* Get columns.
*
* @return array
*/
protected function getColumns()
{
return [
'id',
'name',
'description',
'created_at',
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'wallet_' . time();
}
}
public function dataTable($query)
{
$dataTable = new EloquentDataTable($query);
return $dataTable;
}
public function query(Wallet $model)
{
$query = Wallet::query()->select($this->getColumns())->where('user_id', Auth::user()->id)
->orderBy('created_at', 'desc');
return $this->applyScopes($query);
}
try this @filtration
@jamesriady is correct. The issue here is part of the breaking change on v8.0.
If you are upgrading, you can just find and replace all $this->datatables with datatables() helper function. Thanks!
Fixed. Thanks for the help, guys.
Hello all, I havesuccessfully implemented as said above. But I have to format some of the columns like created_at date in custom format before rendering in view. Previously I used editColumn function for this but in the latest update of yajra datatable how to achieve this ?
Thanks for the information @yajra
Most helpful comment
@jamesriady is correct. The issue here is part of the breaking change on v8.0.
If you are upgrading, you can just find and replace all
$this->datatableswithdatatables()helper function. Thanks!