我是 Laravel 和 Datatables/Editor 的新手。我在另一个 PHP 项目中使用了 DataTables 编辑器,但无法通过Yajra 插件让它在 Laravel 中工作。我按照本教程https://yajrabox.com/docs/laravel-datatables/master/editor-tutorial进行操作。一切都编译得很好,但结果并没有像我预期的那样工作。“编辑”和“删除”按钮显示为灰色且行不可选择。我怀疑我要么不包括 DataTables/Editor 脚本,要么解压缩 Editor.zip(不在教程中)。
我正在使用: - PHP 版本:7.4.2 - Laravel 版本:6.17.1 - Laravel-DataTables Editor PHP 1.9.2(付费版本-zip 文件重命名为 Editor.zip 并包含在根文件夹中)
网页.php
<?php
use App\DataTables\UsersDataTablesEditor;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/users', 'UsersController@index')->name('users.index');
// from https://yajrabox.com/docs/laravel-datatables/master/editor-tutorial
Route::post('/users', 'UsersController@store')->name('users.store');
// from https://yajrabox.com/docs/laravel-datatables/master/editor-usage
Route::post('editor', function(UsersDataTablesEditor $editor) {
return $editor->process(request());
});
用户数据表编辑器.php
<?php
namespace App\DataTables;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;
use Yajra\DataTables\DataTablesEditor;
class UsersDataTableEditor extends DataTablesEditor
{
protected $model = User::class;
/**
* Get create action validation rules.
*
* @return array
*/
public function createRules()
{
return [
'email' => 'required|email|unique:' . $this->resolveModel()->getTable(),
'name' => 'required',
];
}
/**
* Get edit action validation rules.
*
* @param Model $model
* @return array
*/
public function editRules(Model $model)
{
return [
'email' => 'sometimes|required|email|' . Rule::unique($model->getTable())->ignore($model->getKey()),
'name' => 'sometimes|required',
];
}
慕虎7371278