简体   繁体   English

如何在航海者浏览器刀片中添加自定义列?

[英]How to add custom column in voyager browse blade?

I am trying to add a custom column in the voyager browse blade.我正在尝试在航海者浏览刀片中添加自定义列。 I have overridden the view from vendor\tcg\voyager\resources\views\bread\browse.blade.php to resources\views\vendor\voyager\users\browse.blade.php我已经将视图从vendor\tcg\voyager\resources\views\bread\browse.blade.phpresources\views\vendor\voyager\users\browse.blade.php

I have also added the new column No of Outlets you can check in screenshots and also change the query in the controller to get a count of total outlets but now I am facing issues with the data loop of voyager code is below.我还添加了新的网点数量列,您可以在屏幕截图中查看,还可以更改 controller 中的查询以获取网点总数,但现在我面临下面航海者代码的数据循环问题。

@foreach($dataTypeContent as $key=> $data)
<tr>
    @if($showCheckboxColumn)
        <td>
            <input type="checkbox" name="row_id" id="checkbox_{{ $data->getKey() }}" value="{{ $data->getKey() }}">
        </td>
    @endif
    @foreach($dataType->browseRows as $row)
       
        @php
        if ($data->{$row->field.'_browse'}) {
            $data->{$row->field} = $data->{$row->field.'_browse'};
        }
        @endphp
        <td>
            @if (isset($row->details->view))
                @include($row->details->view, ['row' => $row, 'dataType' => $dataType, 'dataTypeContent' => $dataTypeContent, 'content' => $data->{$row->field}, 'action' => 'browse', 'view' => 'browse', 'options' => $row->details])
            @elseif($row->type == 'image')
                <img src="@if( !filter_var($data->{$row->field}, FILTER_VALIDATE_URL)){{ Voyager::image( $data->{$row->field} ) }}@else{{ $data->{$row->field} }}@endif" style="width:100px">
            @elseif($row->type == 'relationship')
                @include('voyager::formfields.relationship', ['view' => 'browse','options' => $row->details])
            @elseif($row->type == 'select_multiple')
                @if(property_exists($row->details, 'relationship'))

                    @foreach($data->{$row->field} as $item)
                        {{ $item->{$row->field} }}
                    @endforeach

                @elseif(property_exists($row->details, 'options'))
                    @if (!empty(json_decode($data->{$row->field})))
                        @foreach(json_decode($data->{$row->field}) as $item)
                            @if (@$row->details->options->{$item})
                                {{ $row->details->options->{$item} . (!$loop->last ? ', ' : '') }}
                            @endif
                        @endforeach
                    @else
                        {{ __('voyager::generic.none') }}
                    @endif
                @endif

                @elseif($row->type == 'multiple_checkbox' && property_exists($row->details, 'options'))
                    @if (@count(json_decode($data->{$row->field})) > 0)
                        @foreach(json_decode($data->{$row->field}) as $item)
                            @if (@$row->details->options->{$item})
                                {{ $row->details->options->{$item} . (!$loop->last ? ', ' : '') }}
                            @endif
                        @endforeach
                    @else
                        {{ __('voyager::generic.none') }}
                    @endif

            @elseif(($row->type == 'select_dropdown' || $row->type == 'radio_btn') && property_exists($row->details, 'options'))

                {!! $row->details->options->{$data->{$row->field}} ?? '' !!}

            @elseif($row->type == 'date' || $row->type == 'timestamp')
                @if ( property_exists($row->details, 'format') && !is_null($data->{$row->field}) )
                    {{ \Carbon\Carbon::parse($data->{$row->field})->formatLocalized($row->details->format) }}
                @else
                    {{ $data->{$row->field} }}
                @endif
            @elseif($row->type == 'checkbox')
                @if(property_exists($row->details, 'on') && property_exists($row->details, 'off'))
                    @if($data->{$row->field})
                        <span class="label label-info">{{ $row->details->on }}</span>
                    @else
                        <span class="label label-primary">{{ $row->details->off }}</span>
                    @endif
                @else
                {{ $data->{$row->field} }}
                @endif
            @elseif($row->type == 'color')
                <span class="badge badge-lg" style="background-color: {{ $data->{$row->field} }}">{{ $data->{$row->field} }}</span>
            @elseif($row->type == 'text')
                @include('voyager::multilingual.input-hidden-bread-browse')
                <div>{{ mb_strlen( $data->{$row->field} ) > 200 ? mb_substr($data->{$row->field}, 0, 200) . ' ...' : $data->{$row->field} }}</div>
            @elseif($row->type == 'text_area')
                @include('voyager::multilingual.input-hidden-bread-browse')
                <div>{{ mb_strlen( $data->{$row->field} ) > 200 ? mb_substr($data->{$row->field}, 0, 200) . ' ...' : $data->{$row->field} }}</div>
            @elseif($row->type == 'file' && !empty($data->{$row->field}) )
                @include('voyager::multilingual.input-hidden-bread-browse')
                @if(json_decode($data->{$row->field}) !== null)
                    @foreach(json_decode($data->{$row->field}) as $file)
                        <a href="{{ Storage::disk(config('voyager.storage.disk'))->url($file->download_link) ?: '' }}" target="_blank">
                            {{ $file->original_name ?: '' }}
                        </a>
                        <br/>
                    @endforeach
                @else
                    <a href="{{ Storage::disk(config('voyager.storage.disk'))->url($data->{$row->field}) }}" target="_blank">
                        Download
                    </a>
                @endif
            @elseif($row->type == 'rich_text_box')
                @include('voyager::multilingual.input-hidden-bread-browse')
                <div>{{ mb_strlen( strip_tags($data->{$row->field}, '<b><i><u>') ) > 200 ? mb_substr(strip_tags($data->{$row->field}, '<b><i><u>'), 0, 200) . ' ...' : strip_tags($data->{$row->field}, '<b><i><u>') }}</div>
            @elseif($row->type == 'coordinates')
                @include('voyager::partials.coordinates-static-image')
            @elseif($row->type == 'multiple_images')
                @php $images = json_decode($data->{$row->field}); @endphp
                @if($images)
                    @php $images = array_slice($images, 0, 3); @endphp
                    @foreach($images as $image)
                        <img src="@if( !filter_var($image, FILTER_VALIDATE_URL)){{ Voyager::image( $image ) }}@else{{ $image }}@endif" style="width:50px">
                    @endforeach
                @endif
            @elseif($row->type == 'media_picker')
                @php
                    if (is_array($data->{$row->field})) {
                        $files = $data->{$row->field};
                    } else {
                        $files = json_decode($data->{$row->field});
                    }
                @endphp
                @if ($files)
                    @if (property_exists($row->details, 'show_as_images') && $row->details->show_as_images)
                        @foreach (array_slice($files, 0, 3) as $file)
                        <img src="@if( !filter_var($file, FILTER_VALIDATE_URL)){{ Voyager::image( $file ) }}@else{{ $file }}@endif" style="width:50px">
                        @endforeach
                    @else
                        <ul>
                        @foreach (array_slice($files, 0, 3) as $file)
                            <li>{{ $file }}</li>
                        @endforeach
                        </ul>
                    @endif
                    @if (count($files) > 3)
                        {{ __('voyager::media.files_more', ['count' => (count($files) - 3)]) }}
                    @endif
                @elseif (is_array($files) && count($files) == 0)
                    {{ trans_choice('voyager::media.files', 0) }}
                @elseif ($data->{$row->field} != '')
                    @if (property_exists($row->details, 'show_as_images') && $row->details->show_as_images)
                        <img src="@if( !filter_var($data->{$row->field}, FILTER_VALIDATE_URL)){{ Voyager::image( $data->{$row->field} ) }}@else{{ $data->{$row->field} }}@endif" style="width:50px">
                    @else
                        {{ $data->{$row->field} }}
                    @endif
                @else
                    {{ trans_choice('voyager::media.files', 0) }}
                @endif
            @else
                @include('voyager::multilingual.input-hidden-bread-browse')
                <span>{{ $data->{$row->field} }}</span>
            @endif
        </td>
        
    @endforeach
    @if($usertype == 'vendor')
        <td class="actions text-right dt-not-orderable">                                        
            
        </td>
    @endif                                           
    <td class="no-sort no-click bread-actions">
        @foreach($actions as $action)
            @if (!method_exists($action, 'massAction'))
                @include('voyager::bread.partials.actions', ['action' => $action])
            @endif
        @endforeach
    </td>
</tr>
@endforeach

Getting data in this format:以这种格式获取数据:

TCG\Voyager\Models\User Object
(
    [guarded:protected] => Array
        (
        )

    [additional_attributes] => Array
        (
            [0] => locale
        )

    [connection:protected] => mysql
    [table:protected] => users
    [primaryKey:protected] => id
    [keyType:protected] => int
    [incrementing] => 1
    [with:protected] => Array
        (
        )

    [withCount:protected] => Array
        (
        )

    [preventsLazyLoading] => 
    [perPage:protected] => 15
    [exists] => 1
    [wasRecentlyCreated] => 
    [escapeWhenCastingToString:protected] => 
    [attributes:protected] => Array
        (
            [id] => 3
            [role_id] => 4
            [name] => Vendor
            [email] => vendor@example.com
            [avatar] => users\February2022\91vfacVxA1krbWq5wiRx.jpg
            [email_verified_at] => 
            [password] => $2y$10$jzpiNV2APkNhBnZzsz0qY.gvfQNFU/NM6l.PStvukFgVKPQpnDR6a
            [remember_token] => 
            [settings] => {"locale":"en"}
            [created_at] => 2022-02-18 19:58:50
            [updated_at] => 2022-02-18 19:58:50
            [phone_no] => 0
            [total_outlets] => 2
        )

    [original:protected] => Array
        (
            [id] => 3
            [role_id] => 4
            [name] => Vendor
            [email] => vendor@example.com
            [avatar] => users\February2022\91vfacVxA1krbWq5wiRx.jpg
            [email_verified_at] => 
            [password] => $2y$10$jzpiNV2APkNhBnZzsz0qY.gvfQNFU/NM6l.PStvukFgVKPQpnDR6a
            [remember_token] => 
            [settings] => {"locale":"en"}
            [created_at] => 2022-02-18 19:58:50
            [updated_at] => 2022-02-18 19:58:50
            [phone_no] => 0
            [total_outlets] => 2
        )

    [changes:protected] => Array
        (
        )

    [casts:protected] => Array
        (
        )

    [classCastCache:protected] => Array
        (
        )

    [attributeCastCache:protected] => Array
        (
        )

    [dates:protected] => Array
        (
        )

    [dateFormat:protected] => 
    [appends:protected] => Array
        (
        )

    [dispatchesEvents:protected] => Array
        (
        )

    [observables:protected] => Array
        (
        )

    [relations:protected] => Array
        (
        )

    [touches:protected] => Array
        (
        )

    [timestamps] => 1
    [hidden:protected] => Array
        (
        )

    [visible:protected] => Array
        (
        )

    [fillable:protected] => Array
        (
        )

    [rememberTokenName:protected] => remember_token
)

在此处输入图像描述

If you have related model, you should add new column to the model fillable.如果您有相关的 model,您应该将新列添加到 model 可填写。

protected $fillable = ['custom_column'];

of course you should add that column to your DB too.当然,您也应该将该列添加到您的数据库中。 Look at this model in: src/Models/DataRow.php在:src/Models/DataRow.php 中查看这个 model

    <?php

namespace TCG\Voyager\Models;

use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Translatable;

class DataRow extends Model
{
    use Translatable;

    protected $table = 'data_rows';

    protected $guarded = [];

    public $timestamps = false;

    protected $translatable = ['display_name'];

    protected $fillable = ['custom_column']; // your custom column

You can add custom fields to voyager by doing this:您可以通过执行以下操作将自定义字段添加到航海者:

  1. Modify your model:修改你的model:
  • add $additional_attributes = ['custom_field'];添加$additional_attributes = ['custom_field'];
  • add public function getCustomFieldAttribute() {return 'what you want';}添加公共function getCustomFieldAttribute() {return 'what you want';}
  1. Modify data_rows table:修改data_rows表:
  • add your custom_field into it将您的custom_field添加到其中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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