This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

gush51974
gush51974

Good day! When I try to save a project in the admin panel, a window pops up with an error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (zen.projects, CONSTRAINT projects_category_id2_foreign FOREIGN KEY (category_id2) REFERENCES categories (id)) (SQL: update projects set subtitle_full = , category_id = 3, category_id2 = NULL, category_id3 = NULL, category_id4 = NULL, category_id5 = NULL, link_comment = , updated_at = 2019-11-30 18:55:03 where id = 23)" on line 624 of D:\OSPanel\domains\zendesign\vendor\laravel\framework\src\Illuminate\Database\Connection.php

In the project table there are fields "category_id", category_id2, etc., which can be NULL. These fields are associated with a categories table. As far as I understand, in addition to communication in the database, it is necessary to register the dependence in the model:

public $belongsTo = [
    'category' => Category::class
];

Or is it not necessary? I'm still digging...

fields.aml:

    category_id:         label: Category 1         type: dropdown         span: left

    category_id2:         label: Category 2         type: dropdown         span: right         #placeholder: '-'

If you uncomment this parameter, an empty value is passed instead of NULL, so I added the getCategoryId2Options (etc.) function to the Project class for getting list of available categories:

public function getCategoryId2Options()
{
    $parents = Category::whereNull('parent_id')->get();
    $result = [];
    $result['NULL'] = '-';
    foreach ($parents as $p) {
        foreach ($p->children as $c) {
            $result[$c->id] = $p->name . ' -> ' . $c->name;
        }
    }

    return $result;
}

But it didn’t work, giving the error above. All I want is that in the admin panel of the project there are several fields for choosing one or more categories.

Last updated

1-1 of 1

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.