无法 insert/update Laravel Backpack PageManager 中的新字段

Not able to insert/update new fields in Laravel Backpack PageManager

按照建议 here 我创建了 app/Models/Page.php。我在 $fillable.

中添加了所有新字段
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use Cviebrock\EloquentSluggable\Sluggable;
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
use Backpack\PageManager\app\Models\Page as OriginalPageModel;

class Page extends OriginalPageModel
{
    use CrudTrait;
    use Sluggable;
    use SluggableScopeHelpers;

     /*
    |--------------------------------------------------------------------------
    | GLOBAL VARIABLES
    |--------------------------------------------------------------------------
    */

    protected $table = 'pages';
    protected $primaryKey = 'id';
    public $timestamps = true;
    // protected $guarded = ['id'];
    protected $fillable = ['template', 'name', 'title', 'slug', 
    'content1-header', 'content1','content2-header','content2',
    'content3-header','content3','content4-header','content4',
    'content5-header','content5','content6-header','content6', 'extras'];
    // protected $hidden = [];
    // protected $dates = [];
    protected $fakeColumns = ['extras'];
...
...
}

并创建了 app/Http/Controllers/Admin/PageCrudController。php 与示例相同的代码。

<?php namespace App\Http\Controllers\Admin;

use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\PageManager\app\Http\Controllers\Admin\PageCrudController as OriginalPageCrudController;
use App\PageTemplates;

// VALIDATION: change the requests to match your own file names if you need form validation
use Backpack\PageManager\app\Http\Requests\PageRequest as StoreRequest;
use Backpack\PageManager\app\Http\Requests\PageRequest as UpdateRequest;

class PageCrudController extends OriginalPageCrudController {

    public function __construct($template_name = false)
    {
        /*
        |--------------------------------------------------------------------------
        | BASIC CRUD INFORMATION
        |--------------------------------------------------------------------------
        */
        $this->crud->setModel("App\Models\Page");
        $this->crud->setRoute(config('backpack.base.route_prefix').'/page');
        $this->crud->setEntityNameStrings('page', 'pages');

        /*
        |--------------------------------------------------------------------------
        | COLUMNS
        |--------------------------------------------------------------------------
        */

        $this->crud->addColumn('name');
        $this->crud->addColumn([
                                'name' => 'template',
                                'type' => 'model_function',
                                'function_name' => 'getTemplateName',
                                ]);
        $this->crud->addColumn('slug');

        /*
        |--------------------------------------------------------------------------
        | FIELDS
        |--------------------------------------------------------------------------
        */

        // In PageManager,
        // - default fields, that all templates are using, are set using $this->addDefaultPageFields();
        // - template-specific fields are set per-template, in the PageTemplates trait;


        /*
        |--------------------------------------------------------------------------
        | BUTTONS
        |--------------------------------------------------------------------------
        */
        $this->crud->addButtonFromModelFunction('line', 'open', 'getOpenButton', 'beginning');
    }

    public function store(StoreRequest $request)
    {
        return parent::storeCrud();
    }

    public function update(UpdateRequest $request)
    {
        return parent::updateCrud();
    }
}

并修改了app/PageTemplates.php

...
...
    private function page()
    {
        $this->crud->addField([   // CustomHTML
                        'name' => 'metas_separator',
                        'type' => 'custom_html',
                        'value' => '<br><h2>Metas</h2><hr>',
                    ]);
   ...
   ...
        $this->crud->addField([   // CustomHTML
                        'name' => 'content_separator',
                        'type' => 'custom_html',
                        'value' => '<br><h2>Content</h2><hr>',
                    ]);
        $this->crud->addField([
                        'name' => 'content1-header',
                        'label' => 'Content 1 Header',
                        'type' => 'text',
                        'placeholder' => 'Your content here',
                    ]);
        $this->crud->addField([
                        'name' => 'content1',
                        'label' => 'Content 1',
                        'type' => 'wysiwyg',
                        'placeholder' => 'Your content here',
                    ]);
        $this->crud->addField([
                        'name' => 'content2-header',
                        'label' => 'Content 2 Header',
                        'type' => 'text',
                        'placeholder' => 'Your content here',
                    ]);
        $this->crud->addField([
                        'name' => 'content2',
                        'label' => 'Content 2',
                        'type' => 'wysiwyg',
                        'placeholder' => 'Your content here',
                    ]);
        $this->crud->addField([
                        'name' => 'content3-header',
                        'label' => 'Content 3 Header',
                        'type' => 'text',
                        'placeholder' => 'Your content here',
                    ]);
        $this->crud->addField([
                        'name' => 'content3',
                        'label' => 'Content 3',
                        'type' => 'wysiwyg',
                        'placeholder' => 'Your content here',
                    ]);
...
...

但是它不更新或插入数据。 我在这里做错了什么?

PageManager 背后的理念是将特定于模板的所有内容存储在数据库的 "extras" 列中,如 JSON。不在他们自己的专栏里面。这样一来,您就不需要在 table 中包含仅适用于一个模板的列。

如果你:

  • 删除 PageCrudController 中的 store() 和 update() 方法;
  • 删除 content1-header', 'content1','content2-header','content2', 'content3-header','content3','content4-header','content4', 'content5-header','content5','content6-header','content6' 来自你的 $fillable 数组

  • 将这些属性添加到您的 PageTemplates 中的每个字段中:

                    'fake'     => true,
                    'store_in' => 'extras',
    

你应该做到这一点:

  • 字段将显示在 create/edit 表单中;
  • 它们的值将存储在 "extras" 列中;

我认为它现在不起作用,因为您已经扩展了 PageCrudController,是的,但是您已经覆盖了 "make it special" - 存储和更新的方法。希望对你有帮助。