Laravel Voyager自定义browse.blade.php:关联表数据无法显示求助
Hey there! I totally get how frustrating this can be when you're new to Voyager—you’ve followed the docs to override controllers and views, tweaked the query to join your tables, but the related data just won’t show up in the browse view. Let’s break down the fixes step by step:
1. Ensure You’re Selecting Related Table Fields
Joining the table doesn’t automatically include its fields in your query result. Even if $query->toSql() shows the JOIN, Voyager’s default select() only grabs fields from your primary table (table_a). You need to explicitly select the fields from table_b you want to display:
// In your overridden index method $query->join('table_b', 'table_a.id', '=', 'table_b.table_a_id') ->select('table_a.*', 'table_b.your_field_name', 'table_b.another_field'); // Add table_b fields here
If there are overlapping field names (like id), use aliases to avoid conflicts:
->select('table_a.*', 'table_b.id as table_b_id', 'table_b.name as table_b_name');
2. Add Related Fields to $dataType->browseRows
Voyager’s $dataType->browseRows is populated from the DataType configuration in the Voyager admin (or the default table schema if you haven’t customized it). Since you joined a new table, these fields aren’t in the default browseRows array—you need to add them manually in your controller:
// After defining your query and fetching $dataType $tableBFields = [ [ 'field' => 'your_field_name', // Or the alias you used in select() 'display_name' => 'Table B Field', 'type' => 'text', // Match the field type (text, number, etc.) 'sortable' => true, // Set to false if you don't want sorting 'nullable' => false, ], // Add more fields as needed ]; foreach ($tableBFields as $field) { $row = new \TCG\Voyager\Models\Row(); $row->fill($field); $dataType->browseRows->push($row); }
Alternatively, you can push stdClass objects directly if you don’t want to use the Row model:
$dataType->browseRows[] = (object)[ 'field' => 'table_b_name', 'display_name' => 'Table B Name', 'type' => 'text', 'sortable' => true, ];
3. (Recommended) Use Voyager’s Native Relationship Support
Instead of manually joining tables, leverage Voyager’s built-in relationship handling—it’s cleaner and more maintainable:
- Define the relationship in your model (e.g.,
TableA.php):
namespace App\Models; use TCG\Voyager\Models\Model; class TableA extends Model { public function tableB() { // Adjust the relationship type (belongsTo, hasMany, etc.) based on your schema return $this->belongsTo(TableB::class, 'table_a_id'); } }
- Add the relationship field in Voyager Admin:
- Go to Voyager > Settings > Data Types
- Select your
table_aDataType - Navigate to the "Browse" tab
- Click "Add Field"
- Set the "Field Type" to
Relationship, then select yourtableBrelationship from the dropdown - Configure the display name and other options, then save
This way, Voyager will automatically fetch and display the related data without you needing to modify the controller query at all!
Final Checks
- Clear Voyager’s cache (run
php artisan voyager:clearin your terminal) to ensure your DataType changes take effect - Double-check that your JOIN conditions are correct (no typos in table/column names)
内容的提问来源于stack exchange,提问作者Avag Sargsyan

