Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel:如何将下拉字段中显示和选择的值存储为输入,而不是id_Php_Laravel_Laravel 7_Backpack For Laravel - Fatal编程技术网

Php Laravel:如何将下拉字段中显示和选择的值存储为输入,而不是id

Php Laravel:如何将下拉字段中显示和选择的值存储为输入,而不是id,php,laravel,laravel-7,backpack-for-laravel,Php,Laravel,Laravel 7,Backpack For Laravel,我使用背包为拉维4.1 表:用户 --------------------------- | id | name | --------------------------- | 1 | Rory Choi | --------------------------- | 2 | Freddie Farrington | --------------------------- | 3 | Cristian Wyatt | ---------

我使用背包为拉维4.1

表:用户

---------------------------
| id |        name        |
---------------------------
| 1  | Rory Choi          |
---------------------------
| 2  | Freddie Farrington |
---------------------------
| 3  | Cristian Wyatt     |
---------------------------
表:文档

---------------------------
| id |        name        |
---------------------------
| 1  | Rory Choi          |
---------------------------
| 2  | Freddie Farrington |
---------------------------
| 3  | Cristian Wyatt     |
---------------------------
我希望存储在表中的值是users表中name的值,而不是id

我要这个:

-------------------------------------------------------
| id |     description     |           name           |
-------------------------------------------------------
| 1  | Something           | Cristian Wyatt           |
-------------------------------------------------------
| 2  | Other thing         | Freddie Farrington       |
-------------------------------------------------------
|    |                     |                          |
-------------------------------------------------------
-------------------------------------------------------
| id |     description     |           name           |
-------------------------------------------------------
| 1  | Something           | 3                        |
-------------------------------------------------------
| 2  | Other thing         | 2                        |
-------------------------------------------------------
|    |                     |                          |
-------------------------------------------------------
不是这个

-------------------------------------------------------
| id |     description     |           name           |
-------------------------------------------------------
| 1  | Something           | Cristian Wyatt           |
-------------------------------------------------------
| 2  | Other thing         | Freddie Farrington       |
-------------------------------------------------------
|    |                     |                          |
-------------------------------------------------------
-------------------------------------------------------
| id |     description     |           name           |
-------------------------------------------------------
| 1  | Something           | 3                        |
-------------------------------------------------------
| 2  | Other thing         | 2                        |
-------------------------------------------------------
|    |                     |                          |
-------------------------------------------------------
DocumentController.php:

它确实显示users表中的名称列表,但不存储名称值

protected function setupCreateOperation()
{
    CRUD::addField([
        'label'     => 'Username',
        'name'      => 'username',
        'type'      => 'select2',
        'entity'    => 'users', // the method that defines the relationship in your Model
        'attribute' => 'name', // foreign key attribute that is shown to user
    ]);
}
文档.php

定义关系的方法

public function users()
{
    return $this->hasOne('User','name','name');
}

我该怎么办?谢谢。

关系不起作用,您可以使用此方法

使用以下命令:

public function users()
{
    return User::where('name',$this->name)->first();
}
而不是

public function users()

    {
        return $this->hasOne('User','name','name');
    }
      

我找到了问题的答案

我需要将此行添加到用户模型:

public $incrementing = false;
并将主键更改为名称:

protected $primaryKey = 'name';
仅供参考:这只是一个示例,我知道使用名称作为主键是不正确的。我想我可以将它用于用户名或其他唯一键(不是递增键)

谢谢


引用:

代替名称字段,为什么不与该用户表建立关系?更合乎逻辑的做法是通过ID与用户建立关系,并从中获取名称(在发生更改时是最新的)?所以documents.name>documents.user_id@Svetoslav我理解,但不知何故我不想那样。如果做不到的话,也许我可以试一下。谢谢。通过ID来建立关系,而不是将属性从表1复制到表2,这是一种更好的方法。在那种情况下,我认为你走错了路。如果明天您决定设置电子邮件,是否也要复制它?我尝试在controller中更改方法,但出现错误,是否需要更改setupCreateOperation中addField()内的属性?删除“name”的属性外键