Error when creating Tailor blog extension

I am writing a seo plugin for tailor but I have a problem when saving data. hope to receive help from you guys.

model: SeoSetting.php

<?php namespace Kenshin\SeoTailor\Models;

use Model;

class SeoSetting extends Model
{
    protected $table = 'kenshin_seo_tailor';

    protected $fillable = ['post_id', 'seo_title', 'seo_description', 'seo_keywords'];

    public $timestamps = true;

    public function tailorEntry()
    {
        return $this->belongsTo(\Tailor\Models\EntryRecord::class, 'post_id');
    }
}

Plugin.php

public function boot()
    {
         Event::listen('backend.form.extendFields', function($widget) {
            if (!$widget->model instanceof EntryRecord) {
                return;
            }
            

            if ($widget->model->blueprint->handle === 'blogPost') {
                return;
            }

            // Thêm tab SEO
            $widget->addTabFields([
                        'seo[seo_title]' => [
                            'label'   => 'SEO Title',
                            'type'    => 'text',
                            'tab'     => 'SEO',
                        ],
                        'seo[seo_description]' => [
                            'label'   => 'SEO Description',
                            'type'    => 'textarea',
                            'tab'     => 'SEO',
                        ],
                        'seo[seo_keywords]' => [
                            'label'   => 'SEO Keywords',
                            'type'    => 'text',
                            'tab'     => 'SEO',
                        ],
                    ]);


                $widget->bindEvent('form.save', function() use ($widget) {
                    $data = post('seo', []);
                    print_r($data );

                    SeoSetting::updateOrCreate(
                        ['post_id' => $widget->model->id],
                        [
                            'seo_title' => $data['seo_title'] ?? '',
                            'seo_description' => $data['seo_description'] ?? '',
                            'seo_keywords' => $data['seo_keywords'] ?? ''
                        ]
                    );
                });
        });
    }

The error is shown as follows when I create a post and try to click the save button

“SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘seo’ in ‘field list’ (SQL: update xc_edcd102e05254e4db07e633ae6c18db6c set title = test, slug = test, is_enabled = 1, draft_mode = 3, primary_attrs = [], published_at_month = 08, content =

tét

, featured_text = , seo = {“seo_title”:“test”,“seo_description”:“test”,“seo_keywords”:“test”}, xc_edcd102e05254e4db07e633ae6c18db6c.updated_at = 2024-08-28 02:31:44 where id = 78)” on line 760 of C:\xampp\htdocs\mori\vendor\laravel\framework\src\Illuminate\Database\Connection.php

Thank you!

Hi @billy

Wow, looks great! Might I suggest taking a look at the Tailor documentation on extending models:

The error is caused because the blueprint will need an seo field definition that creates the relationship to the SeoSetting model.

For example:

seo:
    label: Seo Setting
    comment: Select an SEO setting to use for this blog post
    type: recordfinder
    modelClass: Kenshin\SeoTailor\Models\SeoSetting
    list: $/kenshin/seotailor/models/seosetting/columns.yaml
    maxItems: 1

Hopefully this guides you in the right direction. Let me know if you need further assistance.

Also, consider taking a look at the Building Tailor Fields documentation:

This is a very powerful tool and is the approach I would recommend for what you are trying to achieve. It will give you complete control over the field, list and filter definitions, and the database columns to inject. Then simply add it to every blueprint with type: kenshinseo

Thanks pro for the support. Although I still have to edit in blueprints, the final result is successful.

1 Like