3 of the tables in my database are:
This is the migration of orders table
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:
email
(which is unique) from user Table instead ofuser_id
product_name
(which is also unique) from products Table instead ofproduct_id
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 inprotected function setupCreateOperation()
, it gives me the desired results.
Butprotected function setupListOperation()
displays the referencing id and Not the email id.
Now, you can see I have used
'type' => 'select2'
in user_id'type' => 'select'
in product_idIf I useselect
, it shows the expected column even in setupListOperation. Butselect2
does Not.
My main question is:
How can I display email column from another table instead of referencing ID using select2 in setupListOperation
Using Laravel Framework 8.61.0 and trying this for a long time. Any help is appreciable. Thanks in advance :)
in setupListOperation you use Columns
there is no column in Backpack called "select2", in the end, columns are read-only and users can not input data for columns.
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.