1

正如这里所建议的,我创建了 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',
                    ]);
...
...

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

4

1 回答 1

3

PageManager 背后的理念是将特定于模板的所有内容存储在数据库的“附加”列中,作为 JSON。不在自己的专栏里面。这样一来,您就不需要在表中包含仅满足一个模板的列。

如果你:

  • 删除 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',
    

你应该做到这一点:

  • 这些字段将显示在创建/编辑表单中;
  • 它们的值将存储在“附加”列中;

我相信它现在不起作用,因为您已经扩展了 PageCrudController,是的,但是您已经覆盖了“使其特别”的方法 - 存储和更新。希望能帮助到你。

于 2016-12-23T14:16:30.463 回答