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

1.6k views Asked by At

As suggested here I created app/Models/Page.php. I added all my new fields in $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'];
...
...
}

And created app/Http/Controllers/Admin/PageCrudController.php same code as example.

<?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();
    }
}

And modified 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',
                    ]);
...
...

However it does not update or insert data. What am I doing wrong here?

1

There are 1 answers

3
tabacitu On BEST ANSWER

The philosophy behind PageManager is to store everything that's specific to a template inside the "extras" column in the database, as JSON. Not inside their own column. That way, you don't need to have columns in the table that cater to just one template.

If you:

  • delete the store() and update() methods in your PageCrudController;
  • remove content1-header', 'content1','content2-header','content2', 'content3-header','content3','content4-header','content4', 'content5-header','content5','content6-header','content6' from your $fillable array

  • add these attributes to each of those fields in your PageTemplates:

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

You should achieve exactly that:

  • the fields will show up in the create/edit forms;
  • their values will be stored in the "extras" column;

I believe it doesn't work now because you've extended PageCrudController, yes, but you've overwritten the methods that "make it special" - store and update. Hope it helps.