laravel - 更新关系中的简单文本字段
问题描述
首先,我对背包 CRUD 很陌生,但我没有找到针对我的特定问题的答复。
我有 2 张桌子,users
并且usersprofiles
. 所有用户配置文件都可以通过 CRUD 创建/编辑。当我创建一个新的配置文件时,我会自动创建。我的个人资料belongsTo
auser
和user
hasOne
个人资料。
在我的userprofile
CRUD 面板上,我想编辑属于用户的电子邮件和密码。我添加了显示的字段,我渴望加载相关的用户。
当我创建个人资料时,一切都很好!但是当我想更新时,所有不在配置文件模型上的字段都不会更新。此外,根本不使用观察者。
我的问题很简单,有没有一种简单的方法来更新关系上的简单文本字段。
谢谢
主 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'
],
];
}
}
解决方案
推荐阅读
- python - 使用来自第二个数据帧的值从一个数据帧中进行选择
- javascript - 如何在子组件的 compononentDidMount() 中获取道具
- python - plt.yticks 不显示所有 y 刻度标签
- c# - Unity3D飞行AI运动(直升机)
- python-3.x - 在python中使用reshape函数时如何修复valueerror
- javascript - React 中的手机号码屏蔽
- python - 如何在python中打开href?
- r - 在 natbib 中编译带有引用的 .Rnw 文件时,RStudio 不会写入 .bbl 文件
- ios - Swift 4.2 和 Xcode 10.1 - 提示计算器立即崩溃
- r - 如何按行获取非零元素的平均值,改变条件使用哪些列