表单关联关系

一对一

{tip} Since v1.3.4版本起支持图片以及文件上传表单。

users表和profiles表通过profiles.user_id字段生成一对一关联

  1. CREATE TABLE `users` (
  2. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  4. `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  5. `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  6. `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  9. CREATE TABLE `profiles` (
  10. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  11. `user_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  12. `age` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  13. `gender` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  14. `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  15. `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  16. PRIMARY KEY (`id`)
  17. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

对应的数据模分别为:

  1. <?php
  2. namespace App\Admin\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class User extends Model
  5. {
  6. public function profile()
  7. {
  8. return $this->hasOne(Profile::class);
  9. }
  10. }
  11. class Profile extends Model
  12. {
  13. public function user()
  14. {
  15. return $this->belongsTo(User::class);
  16. }
  17. }

对应的数据仓库为:

  1. <?php
  2. namespace App\Admin\Repositories;
  3. use Dcat\Admin\Repositories\EloquentRepository;
  4. use User as UserModel;
  5. class User extends \Dcat\Admin\Repositories\EloquentRepository
  6. {
  7. protected $eloquentClass = UserModel::class;
  8. }

通过下面的代码可以关联在一个form里面:

{tip} 实例化数据仓库时需要传入关联模型定义的关联名称,相当于主动使用Eloquent\Model::with方法。

  1. use App\Admin\Repositories\User;
  2. // 注意这里实例化数据仓库`User`时必须传入"profile",否则将无法关联"profiles"表数据
  3. $form = Form::make(new User('profile'), function (Form $form) {
  4. $form->display('id');
  5. $form->text('name');
  6. $form->text('email');
  7. $form->text('profile.age');
  8. $form->text('profile.gender');
  9. $form->datetime('created_at');
  10. $form->datetime('updated_at');
  11. });

如果你不想使用数据仓库,也可以直接使用模型

  1. use App\Admin\Models\User;
  2. // 注意这里是直接使用模型,没有使用数据仓库
  3. $form = Form::make(User::with('profile'), function (Form $form) {
  4. $form->display('id');
  5. ...
  6. });

一对多

一对多的使用请参考文档表单字段的使用-一对多

多对多

下面以项目内置的角色管理模块的角色绑定权限功能为例来演示多对多关联模型的用法

模型Role

  1. <?php
  2. namespace Dcat\Admin\Models;
  3. use Dcat\Admin\Traits\HasDateTimeFormatter;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  6. class Role extends Model
  7. {
  8. use HasDateTimeFormatter;
  9. /**
  10. * 定义你的关联模型.
  11. *
  12. * @return BelongsToMany
  13. */
  14. public function permissions(): BelongsToMany
  15. {
  16. $pivotTable = 'admin_role_permissions'; // 中间表
  17. $relatedModel = Permission::class; // 关联模型类名
  18. return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'permission_id');
  19. }
  20. }
  1. use Dcat\Admin\Models\Permission;
  2. // 实例化数据仓库时传入 permissions,则会自动关联关联模型的数据
  3. // 这里传入 permissions 关联权限模型的数据
  4. $repository = new Role(['permissions']);
  5. return Form::make($repository, function (Form $form) {
  6. $form->display('id', 'ID');
  7. $form->text('slug', trans('admin.slug'))->required();
  8. $form->text('name', trans('admin.name'))->required();
  9. // 这里的数据会自动保存到关联模型中
  10. $form->tree('permissions')
  11. ->nodes(function () {
  12. return (new Permission())->allNodes();
  13. })
  14. ->customFormat(function ($v) {
  15. if (!$v) return [];
  16. // 这一步非常重要,需要把数据库中查出来的二维数组转化成一维数组
  17. return array_column($v, 'id');
  18. });
  19. ...
  20. });

如果你不想使用数据仓库,也可以直接使用模型

  1. use Dcat\Admin\Models\Role;
  2. // 注意这里是直接使用模型,没有使用数据仓库
  3. $form = Form::make(Role::with('permissions'), function (Form $form) {
  4. $form->display('id');
  5. ...
  6. });

最终效果如下

表单关联关系 - 图1

关联模型名称为驼峰风格

如果你的关联模型名称的命名是驼峰风格,那么使用的时候需要转化为下划线风格命名

例如

  1. class User extend Model
  2. {
  3. public function userProfile()
  4. {
  5. return ...;
  6. }
  7. }

使用

  1. return Form::make(User::with(['userProfile']), function (Form $form) {
  2. ...
  3. // 注意这里必须使用下划线风格命名,否则将无法显示编辑数据
  4. $form->text('user_profile.postcode');
  5. $form->text('user_profile.address');
  6. });