简体   繁体   English

显示另一个表中的电子邮件列而不是引用 Laravel 背包中的 ID

[英]Display email column from another table instead of referencing ID in laravel backpack

3 of the tables in my database are:我的数据库中的 3 个表是:

  1. Orders订单
  2. Products产品
  3. User用户

This is the migration of orders table这是orders表的迁移

public function up()
{
    Schema::create('orders', function (Blueprint $table) {
        $table->id();
        $table->foreignId('user_id')->constrained();  // FK to Users table
        $table->foreignId('product_id')->constrained();  // FK to product table
        // ...
        $table->timestamps();
    });
}

Now when I am Listing all the data in my Admin panel, I want to display:现在,当我在管理面板中列出所有数据时,我想显示:

  1. email (which is unique) from user Table instead of user_id来自用户表而不是user_id email (这是唯一的)
  2. product_name (which is also unique) from products Table instead of product_id来自 products 表的product_name (也是唯一的)而不是product_id

app/Http/Controllers/Admin/OrderCrudController.php app/Http/Controllers/Admin/OrderCrudController.php

[
            'name'  => 'user_id', // Table column which is FK for Customer table
            'label' => 'User', // Label for HTML form field
            'type'  => 'select2',  // HTML element which displaying transactions
            
            'placeholder' => 'Select a User', // placeholder for the select
            'allows_null' => true,
            'attribute' => 'email', // Column which user see in select box
            'minimum_input_length' => 1,
            'hint' => 'The person who ordered the product',
        ],
        [
            'name'  => 'product_id',
            'label' => 'Product',
            'type' => 'select',

            'placeholder' => 'Select a Product', // placeholder for the select
            'allows_null' => true,
            'attribute' => 'name', // Column which user see in select box
            'minimum_input_length' => 1,
            'hint'  => 'The product which was ordered',
        ],
]

When I use the above code in protected function setupCreateOperation() , it gives me the desired results.当我在protected function setupCreateOperation()使用上面的代码时,它给了我想要的结果。

在此处输入图片说明

But protected function setupListOperation() displays the referencing id and Not the email id.但是protected function setupListOperation()显示引用 ID 而不是电子邮件 ID。

在此处输入图片说明


Now, you can see I have used现在,你可以看到我已经使用了

  1. 'type' => 'select2' in user_id 'type' => 'select2' user_id 中的'type' => 'select2'
  2. 'type' => 'select' in product_id 'type' => 'select' in product_id

If I use select , it shows the expected column even in setupListOperation .如果我使用select ,它甚至在setupListOperation 中也会显示预期的列。 But select2 does Not.但是select2没有。

My main question is:我的主要问题是:

How can I display email column from another table instead of referencing ID using select2 in setupListOperation如何显示另一个表中的电子邮件列而不是在setupListOperation 中使用select2引用 ID


Using Laravel Framework 8.61.0 and trying this for a long time.使用 Laravel Framework 8.61.0 并尝试了很长时间。 Any help is appreciable.任何帮助都是可观的。 Thanks in advance :)提前致谢 :)

in setupListOperation you use Columns在 setupListOperation 中,您使用Columns

there is no column in Backpack called "select2", in the end, columns are read-only and users can not input data for columns. Backpack 中没有名为“select2”的列,最终列是只读的,用户无法为列输入数据。

to display user's email, you can use select column:要显示用户的电子邮件,您可以使用 选择列:

[
   
   'label'     => 'email', // Table column heading
   'type'      => 'select',
   'name'      => 'user_id',
   'key'=>'email' 
   'entity'    => 'user', // the method that defines the relationship in your Model
   'attribute' => 'email', // foreign key attribute that is shown to user
   'model'     => User::class, // foreign key model
],

make sure you have user() relation in your Order model.确保您的 Order 模型中有 user() 关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM