Voyager: Displaying related entities

Created on 15 Mar 2018  Â·  2Comments  Â·  Source: the-control-group/voyager

  • Laravel Version: 5.5.39
  • Voyager Version: 1.0.16
  • PHP Version: 7.1.14
  • Database Driver & Version: mysql, MariaDB 10.0.34

Description:

Can I have an easier access to the related DB entities than just one textual value of one of the entity's columns?

Currently, I can only output an ID of the related entity, and then manually click on that entities view from the menu and search by the copied ID. It is slow and cumbersome.

Steps To Reproduce:

  1. Create table orders;
  2. create table order_items;
  3. for orders, create BREAD with a hasMany relationship with order_items;
  4. for order_items, create BREAD with a blongsTo relationship with orders;
  5. add a row in orders and two rows in order_items related to the one orders row;
  6. open the admin page for listing order_items;
  7. ---> can I have a link for each item to the related order view?
  8. open the admin page for listing orders;
  9. ---> can I have two links in the order_items column to the related order items views?
  10. ---> can I have more info about the order items than just the one configured order_items column?
  11. click "View" for the order;
  12. ---> can I have more info listed for each of the related order items than just the one configured column?

Most helpful comment

Maybe someone finds this useful – it adds links to the (sub-)values of the related table.

You have to copy the relationship.blade.php from vendor/tcg/voyager/resources/views/formfields/relationship.blade.php into resources/assets/views/vendor/voyager/formfields/relationship.blade.php and apply the following patch:

diff --git a/resources/views/vendor/voyager/formfields/relationship.blade.php b/resources/views/vendor/voyager/formfields/relationship.blade.php
index cdb436d..c052cac 100644
--- a/resources/views/vendor/voyager/formfields/relationship.blade.php
+++ b/resources/views/vendor/voyager/formfields/relationship.blade.php
@@ -19,7 +19,10 @@
                @endphp

                @if(isset($query))
-                   <p>{{ $query->{$options->label} }}</p>
+                   <p><a href="{{route(
+                           'voyager.'.Voyager::model('DataType')->whereName($options->table)->first()->slug.'.show',
+                           $query->{$options->key}
+                       )}}">{{ $query->{$options->label} }}</a></p>
                @else
                    <p>No results</p>
                @endif
@@ -63,6 +66,16 @@
                    $relationshipData = (isset($data)) ? $data : $dataTypeContent;
                    $model = app($options->model);
                    $selected_values = $model::where($options->column, '=', $relationshipData->id)->pluck($options->label)->all();
+
+                   $routeName = 'voyager.'.Voyager::model('DataType')->whereName($options->table)->first()->slug.'.show';
+                   $items = $model::where($options->column, '=', $relationshipData->id)->get()->reduce(
+                       function ($links, $item) use ($options, $routeName) {
+                           $links[] = [
+                               'label' => $item->{$options->label},
+                               'url' => route($routeName, $item->id)
+                           ];
+                           return $links;
+                       });
                @endphp

                @if($view == 'browse')
@@ -80,8 +93,8 @@
                        <p>No results</p>
                    @else
                        <ul>
-                           @foreach($selected_values as $selected_value)
-                               <li>{{ $selected_value }}</li>
+                           @foreach($items as $item)
+                       <li><a href="{{$item['url']}}">{{$item['label']}}</a></li>
                            @endforeach
                        </ul>
                    @endif

All 2 comments

Maybe someone finds this useful – it adds links to the (sub-)values of the related table.

You have to copy the relationship.blade.php from vendor/tcg/voyager/resources/views/formfields/relationship.blade.php into resources/assets/views/vendor/voyager/formfields/relationship.blade.php and apply the following patch:

diff --git a/resources/views/vendor/voyager/formfields/relationship.blade.php b/resources/views/vendor/voyager/formfields/relationship.blade.php
index cdb436d..c052cac 100644
--- a/resources/views/vendor/voyager/formfields/relationship.blade.php
+++ b/resources/views/vendor/voyager/formfields/relationship.blade.php
@@ -19,7 +19,10 @@
                @endphp

                @if(isset($query))
-                   <p>{{ $query->{$options->label} }}</p>
+                   <p><a href="{{route(
+                           'voyager.'.Voyager::model('DataType')->whereName($options->table)->first()->slug.'.show',
+                           $query->{$options->key}
+                       )}}">{{ $query->{$options->label} }}</a></p>
                @else
                    <p>No results</p>
                @endif
@@ -63,6 +66,16 @@
                    $relationshipData = (isset($data)) ? $data : $dataTypeContent;
                    $model = app($options->model);
                    $selected_values = $model::where($options->column, '=', $relationshipData->id)->pluck($options->label)->all();
+
+                   $routeName = 'voyager.'.Voyager::model('DataType')->whereName($options->table)->first()->slug.'.show';
+                   $items = $model::where($options->column, '=', $relationshipData->id)->get()->reduce(
+                       function ($links, $item) use ($options, $routeName) {
+                           $links[] = [
+                               'label' => $item->{$options->label},
+                               'url' => route($routeName, $item->id)
+                           ];
+                           return $links;
+                       });
                @endphp

                @if($view == 'browse')
@@ -80,8 +93,8 @@
                        <p>No results</p>
                    @else
                        <ul>
-                           @foreach($selected_values as $selected_value)
-                               <li>{{ $selected_value }}</li>
+                           @foreach($items as $item)
+                       <li><a href="{{$item['url']}}">{{$item['label']}}</a></li>
                            @endforeach
                        </ul>
                    @endif

@JanisE Should be a hook or a pull request. But needs some improvement. We need to test if we can retrieve the relation in DataType before using it. It trows error if no bread was created for the relation.

Trowed when no BREAD:
Trying to get property 'slug' of non-object (in relationship.blade.php)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zzpwestlife picture zzpwestlife  Â·  3Comments

vaggelis2018 picture vaggelis2018  Â·  3Comments

abacram picture abacram  Â·  3Comments

rayqiri picture rayqiri  Â·  3Comments

raoasifraza1 picture raoasifraza1  Â·  3Comments