首页 > 解决方案 > 更新关系中的简单文本字段

问题描述

首先,我对背包 CRUD 很陌生,但我没有找到针对我的特定问题的答复。

我有 2 张桌子,users并且usersprofiles. 所有用户配置文件都可以通过 CRUD 创建/编辑。当我创建一个新的配置文件时,我会自动创建。我的个人资料belongsToauseruser hasOne个人资料。

在我的userprofileCRUD 面板上,我想编辑属于用户的电子邮件和密码。我添加了显示的字段,我渴望加载相关的用户。

当我创建个人资料时,一切都很好!但是当我想更新时,所有不在配置文件模型上的字段都不会更新。此外,根本不使用观察者。

我的问题很简单,有没有一种简单的方法来更新关系上的简单文本字段。

谢谢

主 CRUD 控制器:

class PatientCrudController extends PatientAidantBaseController
{
    use ReviseOperation;

    /**
     * Setup
     *
     * @return void
     */
    public function setup()
    {
        $this->crud->setModel(Patient::class);
        $this->crud->with('user');
        $this->crud->setRoute(config('backpack.base.route_prefix') . '/patient');
        $this->crud->setEntityNameStrings('patient', 'patients');
    }

    /**
     * SetupListOperation
     *
     * @return void
     */
    protected function setupListOperation()
    {
        parent::setupListOperation();

        $this->crud->addColumn(
            [
                'name' => 'numeroHedonix',
                'label' => "Numéro Hedonix",
                'type' => 'text'
            ]
        );
    }

    /**
     * SetupCreateOperation
     *
     * @return void
     */
    protected function setupCreateOperation()
    {
        $this->crud->addFields([
            [   'name' => 'numeroHedonix',
                'type' => 'text',
                'label' => 'Numéro Hedonix',
                'default' => Patient::getNextHedonixNumber(),
                'wrapper' => [
                    'class' => 'form-group col-md-4',

                ],
                'attributes' => [
                    'readonly' => 'readonly',
                ],
                'tab' => 'Informations patient'
            ],

        ]);

        $this->crud->addFields($this->baseCreationFields());
        $this->crud->addFields([
            [   'name' => 'NIP',
                'type' => 'text',
                'label' => 'NIP',
                'wrapper' => [
                    'class' => 'form-group col-md-6',

                ],
                'tab' => 'Informations patient'
            ],
            [   'name' => 'NID',
                'type' => 'text',
                'label' => 'NID',
                'wrapper' => [
                    'class' => 'form-group col-md-6',

                ],
                'tab' => 'Informations patient'
            ],
        ]);

        $this->crud->setValidation(PatientRequest::class);
    }

    /**
     * SetupUpdateOperation
     *
     * @return void
     */
    protected function setupUpdateOperation()
    {
        $this->setupCreateOperation();
    }
}

这扩展了此控制器以使功能和字段相互化:

<?php

namespace App\Http\Controllers\Admin;

use App\User;
use App\Enums\SexePatient;
use App\Enums\NiveauEtudePatient;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;

abstract class PatientAidantBaseController extends CrudController
{
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;

    /**
     * SetupListOperation
     *
     * @return void
     */
    protected function setupListOperation()
    {
        $this->crud->addColumn(
            [
                'name' => 'prenom',
                'label' => "Prénom",
                'type' => 'text'
            ]
        );
        $this->crud->addColumn(
            [
                'name' => 'nom',
                'label' => "Nom",
                'type' => 'text'
            ]
        );
        $this->crud->addColumn(
            [
                'name' => 'dateDeNaissance',
                'label' => "Date de naissance",
                'type' => 'date'
            ]
        );
    }
protected function baseCreationFields()
    {
        return [
            [   'name' => 'nom',
                'type' => 'text',
                'label' => 'Nom',
                'wrapper' => [
                    'class' => 'form-group col-md-4'
                ],
                'tab' => 'Informations patient'
            ],
            [   'name' => 'prenom',
                'type' => 'text',
                'label' => 'Prénom',
                'wrapper' => [
                    'class' => 'form-group col-md-4'
                ],
                'tab' => 'Informations patient'
            ],
            [   'name' => 'sexe',
                'label' => 'Sexe',
                'type' => 'select_from_array',
                'options' => SexePatient::toSelectArray(),
                'allows_null' => false,
                'default' => 'M',
                'wrapper' => [
                    'class' => 'form-group col-md-4'
                ],
                'tab' => 'Informations patient'
            ],
            [   'name' => 'dateDeNaissance',
                'type' => 'date',
                'label' => 'Date de naissance',
                'wrapper' => [
                    'class' => 'form-group col-md-4'
                ],
                'tab' => 'Informations patient'
            ],
            [   'name' => 'dateDeDeces',
                'type' => 'date',
                'label' => 'Date de décès',
                'allows_null' => true,
                'default' => null,
                'wrapper' => [
                    'class' => 'form-group col-md-4'
                ],
                'tab' => 'Informations patient'
            ],
            [   'name' => 'niveauEtude',
                'label' => "Niveau d'étude",
                'type' => 'select_from_array',
                'options' => NiveauEtudePatient::toSelectArray(),
                'allows_null' => true,
                'default' => null,
                'tab' => 'Informations patient'
            ],
            [   'name' => 'user_id',
                'type' => 'hidden',
                'default' => null
            ],
            [   'name' => 'user.email',
                'label' => 'Adresse email',
                'type' => 'email',
                'tab' => 'Informations patient'
                //'tab' => 'Création du compte'
            ],
            [   'name' => 'password',
                'label' => 'Mot de passe',
                'type' => 'password',
                'tab' => 'Informations patient'
                //'tab' => 'Création du compte'
            ],
            [   'name' => 'password_confirmation',
                'label' => 'Confirmation mot de passe',
                'type' => 'password',
                'tab' => 'Informations patient'
                //'tab' => 'Création du compte'
            ],
        ];
    }
}

标签: laravelcrudlaravel-backpackeloquent-relationship

解决方案


推荐阅读