简体   繁体   中英

Laravel-Backpack Getting data of 2 tables by join

I'm new using backpack for laravel and I'm trying to understand how could show data from DB in the default view that backpack use to display rows. I already read the documentation from the site but it's really poor, and have a lot of questions.

I have 2 models linked by join, for example:

Table 1                 Table 2
-id                      -id
-name                    -phone
-age                     -description
-table2_id

How can I display the attributes from table 2 in table 1 list view?. Backpack haves this view to list elements of modules

背包列表视图

I want to see on that table the combination of the 2 tables...Any code that could help me?. Thank you for your help.

Backpack creates CRUD Panels for you Eloquent Models. Not your database tables. So in order to have columns showing up that show elements from another table, you need to properly define the relationships between those Models . Then you can use the select column to show that connected entry.

  [
            'name'  => 'rm_id',
            'label' => 'Relationship Manager',
            'type'  => 'select2',
            'model'=>config('permission.models.role'),
            'options'   => (function ($query) {
                return $query->where('roles.name', 'Relationship Manager')
                    ->join('model_has_roles', 'model_has_roles.role_id','=','roles.id')
                    ->join('users', 'model_has_roles.model_id','=','users.id')
                    ->select('users.id','users.name')->get();
            }),
            'attribute'=>'name',
        ],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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