php 如何在voyager browse blade中添加自定义栏目?

xnifntxz  于 2023-01-19  发布在  PHP
关注(0)|答案(2)|浏览(109)

我正尝试在voyager browse blade中添加自定义列。我已将视图从vendor\tcg\voyager\resources\views\bread\browse.blade.php覆盖为resources\views\vendor\voyager\users\browse.blade.php
我还添加了新列插座数量您可以检查屏幕截图,也可以更改控制器中的查询,以获得插座总数,但现在我面临的问题与voyager代码的数据循环如下。

@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

获取此格式的数据:

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
)

2ic8powd

2ic8powd1#

如果你有相关的模型,你应该添加新的列到模型可填充.

protected $fillable = ['custom_column'];

当然你也应该把这个列添加到你的DB中。2看看这个模型:src/Models/DataRow.php

<?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
5kgi1eie

5kgi1eie2#

您可以通过以下操作向voyager添加自定义字段:
1.修改您的模型:

  • 相加$additional_attributes = ['custom_field'];
  • 添加公共function getCustomFieldAttribute() {return 'what you want';}

1.修改data_rows表:

  • 把你的custom_field加进去

相关问题