如何在Voyager浏览刀片中添加自定义列?
我正在尝试在Voyager浏览刀片中添加自定义列。 I have overridden the view from vendor\tcg\voyager\resources\views\bread\browse.blade.php
to resources\views\vendor\voyager\users\browse.blade.php< /code>
我还添加了新的列 no oferlets 您可以在屏幕截图中检查并更改控制器中的查询以获取总插座,但现在我面临数据循环的问题以下是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] => [email protected]
[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] => [email protected]
[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
)
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
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.
@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] => [email protected]
[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] => [email protected]
[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
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过这样做来将自定义字段添加到Voyager:
$ shoded_attributes = ['custic_field'];
函数getCustomfieldAttribute(){返回'wart you way'you way';};}
data_rows
表:custom_field
添加到其中You can add custom fields to voyager by doing this:
$additional_attributes = ['custom_field'];
function getCustomFieldAttribute() {return 'what you want';}
data_rows
table:custom_field
into it如果您有相关的模型,则应将新列添加到模型可填充。
当然,您也应该将该列也添加到数据库中。
在以下方式查看此模型:src/models/datarow.php
If you have related model, you should add new column to the model fillable.
of course you should add that column to your DB too.
Look at this model in: src/Models/DataRow.php