I have setup datatables with editor from yajra/laravel-datatables-editor package. I can get it where the datatable comes up and works but when i try to edit a field it does not update and I get a 500 error on the ajax call with this message "Type error: Argument 1 passed to App\DataTables\DioceseDataTablesEditor::editRules() must be an instance of Illuminate\Database\Eloquent\Model, null given"
I followed the user tutorial that is given with the package just using a different table with different rows. Looking for some help as I am stumped to what I am missing.
Here is my DioceseDataTablesEditor.php file which is where the error occurs.
namespace App\DataTables;
use App\Diocese;
use Illuminate\Validation\Rule;
use Yajra\DataTables\DataTablesEditor;
use Illuminate\Database\Eloquent\Model;
class DioceseDataTablesEditor extends DataTablesEditor
{
protected $model = Diocese::class;
/**
* Get create action validation rules.
*
* @return array
*/
public function createRules()
{
return [
//'diocese' => 'required',
];
}
/**
* Get edit action validation rules.
*
* @param Model $model
* @return array
*/
public function editRules(Model $model) //This is where the error occurs
{
return [
// 'diocese' => 'sometimes|required',
];
}
/**
* Get remove action validation rules.
*
* @param Model $model
* @return array
*/
public function removeRules(Model $model)
{
return [];
}
/**
* Pre-update action event hook.
*
* @param Model $model
* @return array
*/
public function updating(Model $model, array $data)
{
return $data;
}
}
The thing I notice is in the ajax call it never send the unique id of the row which is a column called link which I have set in the Diocese model. This is what I think may be the issue. It only sends over the following to fields in the post.
action:edit data[link][denominat]:Other //it is the actual word link and not the link value with a an integer.
Here is my javascript for it.
$(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': '{{csrf_token()}}'
}
});
var editor = new $.fn.dataTable.Editor({
ajax: "/admin/diocese",
table: "#diocese",
display: "bootstrap",
fields: [
{ label: 'link', name: 'link' },
{ label: 'denominat', name: 'denominat', type: 'select', options: [
"Catholic", "Episcopal", "Methodist", "PNCC", "American Baptist", "ELCA", "AMEZC", "Presbyterian", "Individual", "Other", "AME Church", "ACNA"
] },
{ label: 'abbrev', name: 'abbrev' },
{ label: 'diocese', name: 'diocese' },
{ label: 'addrone', name: 'addrone' },
{ label: 'addrtwo', name: 'addrtwo' },
{ label: 'city', name: 'city' },
{ label: 'state', name: 'state' },
{ label: 'zipcode', name: 'zipcode' },
{ label: 'telephone', name: 'telephone' },
]
});
$('#diocese').on('click', 'tbody td:not(:first-child)', function (e) {
editor.inline(this);
});
{{$dataTable->generateScripts()}}
})
Any help would be appreciated.
Edit. Here is controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\DataTables\DioceseDataTable;
use App\DataTables\DioceseDataTablesEditor;
use Illuminate\Support\Facades\Schema;
class DioceseController extends Controller
{
public function index(DioceseDataTable $dataTable)
{
return $dataTable->render('admin.diocese.index');
}
public function store(DioceseDataTablesEditor $editor)
{
return $editor->process(request());
}
public function getTableColumns($table)
{
return Schema::getColumnListing($table);
}
}
I was able to solve this and will leave the answer for future reference. Thinking the data being sent as data[link][denominat] = "Others" was wrong I setup a simple test form the send in that data as an ajax post but have it look how I thought it should look which would be data[42456][denominat] = "Others" and when I did that it worked just fine.
So then I looked more at the editor docs and I had to let the editor know the name of my primary key which was "link" So the changed by code to what is below.
var editor = new $.fn.dataTable.Editor({
ajax: "/admin/diocese",
table: "#diocese",
idSrc: "link", // ##added this line
display: "bootstrap",
fields: [
{ label: 'link', name: 'link' },
{ label: 'denominat', name: 'denominat', type: 'select', options: [
"Catholic", "Episcopal", "Methodist", "PNCC", "American Baptist", "ELCA", "AMEZC", "Presbyterian", "Individual", "Other", "AME Church", "ACNA"
] },
{ label: 'abbrev', name: 'abbrev' },
{ label: 'diocese', name: 'diocese' },
{ label: 'addrone', name: 'addrone' },
{ label: 'addrtwo', name: 'addrtwo' },
{ label: 'city', name: 'city' },
{ label: 'state', name: 'state' },
{ label: 'zipcode', name: 'zipcode' },
{ label: 'telephone', name: 'telephone' },
]
});