我已经使用 yajra/laravel-datatables-editor 包中的编辑器设置了数据表。我可以在数据表出现并工作的地方得到它,但是当我尝试编辑一个字段时它不会更新,并且我在 ajax 调用中收到 500 错误,并显示此消息“类型错误:参数 1 传递给 App\DataTables\DioceseDataTablesEditor: :editRules() 必须是 Illuminate\Database\Eloquent\Model 的实例,给定 null"
我遵循了随包提供的用户教程,只是使用了具有不同行的不同表。寻找一些帮助,因为我对我所缺少的东西感到困惑。
这是我的 DioceseDataTablesEditor.php 文件,这是发生错误的地方。
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;
}
}
我注意到在 ajax 调用中它永远不会发送行的唯一 ID,这是我在 Diocese 模型中设置的称为链接的列。这就是我认为可能的问题。它仅将以下内容发送到帖子中的字段。
action:edit data[link][denomiat]:Other //它是实际的单词链接,而不是带有整数的链接值。
这是我的javascript。
$(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()}}
})
任何帮助,将不胜感激。
编辑。这里是控制器
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);
}
}