无法在Laravel Backpack PageManager中插入/更新新字段

时间:2016-12-23 08:36:53

标签: backpack-for-laravel

根据建议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',
                    ]);
...
...

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

1 个答案:

答案 0 :(得分:3)

PageManager背后的理念是将模板中特定的所有内容存储在&#34; extras&#34;数据库中的列,如JSON。不在他们自己的专栏里面。这样,您就不需要在表格中提供仅适合一个模板的列。

如果你:

  • 删除PageCrudController;
  • 中的store()和update()方法
  • 删除content1-header&#39;,&#39; content1&#39;,&#39; content2-header&#39;,&#39; content2&#39;, &#39; content3报头&#39;&#39; content3&#39;,&#39; content4报头&#39;&#39; content4&#39 ;, &#39; content5报头&#39;&#39; content5&#39;,&#39; content6报头&#39;&#39; content6&#39;来自您的$ fillable数组

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

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

你应该完全达到这个目的:

  • 字段将显示在创建/编辑表单中;
  • 他们的价值将存储在&#34; extras&#34;柱;

我相信它现在不起作用,因为你已经扩展了PageCrudController,是的,但是你已经覆盖了那些让它变得特别的方法&#34; - 存储和更新。希望它有所帮助。