r9 ingenieria
r9 ingenieria

Reputation: 43

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

backpack-list-view

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

Upvotes: 2

Views: 5476

Answers (2)

kush
kush

Reputation: 645

  [
            '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',
        ],

Upvotes: 0

tabacitu
tabacitu

Reputation: 6193

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.

Upvotes: 1

Related Questions