laravel 数据表编辑器出现 500 错误
500 error with datatables editor with laravel
我有 yajra/laravel-datatables-editor 包中编辑器的设置数据table。我可以在数据 table 出现并工作的地方得到它,但是当我尝试编辑一个字段时它不会更新并且我在 ajax 调用中收到 500 错误消息 "Type error: Argument 1 passed to App\DataTables\DioceseDataTablesEditor::editRules() must be an instance of Illuminate\Database\Eloquent\Model, null given"
我遵循了随包提供的用户教程,只是使用不同的 table 和不同的行。寻找一些帮助,因为我对我所缺少的东西感到困惑。
这是我的 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 模型中设置的名为 link 的列。这就是我认为可能是问题所在。它仅将以下内容发送到 post.
中的字段
动作:编辑
数据[link][面额]:其他
//它是实际的单词 link 而不是带有整数的 link 值。
这是我的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);
}
}
我已经解决了这个问题,我会留下答案以备将来参考。认为作为 data[link][denominat] = "Others" 发送的数据是错误的,我设置了一个简单的测试表单,将数据作为 ajax post 发送,但是它看起来像我认为它应该看起来像 data[42456][denominat] = "Others" 当我这样做时它工作得很好。
然后我查看了更多编辑器文档,我不得不让编辑器知道我的主键名称 "link" 所以代码更改为下面的内容。
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' },
]
});
我有 yajra/laravel-datatables-editor 包中编辑器的设置数据table。我可以在数据 table 出现并工作的地方得到它,但是当我尝试编辑一个字段时它不会更新并且我在 ajax 调用中收到 500 错误消息 "Type error: Argument 1 passed to App\DataTables\DioceseDataTablesEditor::editRules() must be an instance of Illuminate\Database\Eloquent\Model, null given"
我遵循了随包提供的用户教程,只是使用不同的 table 和不同的行。寻找一些帮助,因为我对我所缺少的东西感到困惑。
这是我的 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 模型中设置的名为 link 的列。这就是我认为可能是问题所在。它仅将以下内容发送到 post.
中的字段动作:编辑 数据[link][面额]:其他 //它是实际的单词 link 而不是带有整数的 link 值。
这是我的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);
}
}
我已经解决了这个问题,我会留下答案以备将来参考。认为作为 data[link][denominat] = "Others" 发送的数据是错误的,我设置了一个简单的测试表单,将数据作为 ajax post 发送,但是它看起来像我认为它应该看起来像 data[42456][denominat] = "Others" 当我这样做时它工作得很好。
然后我查看了更多编辑器文档,我不得不让编辑器知道我的主键名称 "link" 所以代码更改为下面的内容。
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' },
]
});