我在使用 Laravel Voyager v1.2 时遇到问题。我需要根据条件轻松过滤选择下拉列表中的相关选项列表。假设我有与类别相关的帖子,但我不想在选择下拉列表中列出所有类别,而只想列出事件的类别。
- 我尝试强制 Voyager 采用我自己的方法在 Post 模型中建立关系:
public function category_id() {
return $this->belongsTo('Post::class')->where('active',1);
}
- 还尝试在 BREAD JSON 选项中为类别字段定义范围:
{
"relationship": {
"key": "id",
"label": "name",
"scopes": [
"active"
]
}
}
发布模型部分:
public function scopeActive($query) {
return $query->where('active', 1);
}
这就是我的 Post 类的样子(我实际上不需要过滤事件类别,而是按 ID 过滤食物类别):
public function soup1() {
return $this->belongsTo('App\Meal')->where('meal_category_id',1);
}
public function soup2() {
return $this->belongsTo('App\Meal')->where('meal_category_id',1);
}
public function mealy() {
return $this->belongsTo('App\Meal')->where('meal_category_id',7);
}
public function scopeSoups($query)
{
return $query->where('meal_category_id', 1);
}
public function scopeMealy($query)
{
return $query->where('meal_category_id', 7);
}
我想接收经过筛选的选项列表,而不是所有模型行。
最佳答案
您可以通过覆盖 voyager 中的 Blade 轻松完成此操作:
第 1 步:
复制relationship.blade.php
来自: /"YourProject"/vendor/tcg/voyager/resources/views/formfields/relationship.blade.php
收件人: /"YourProject"/resources/views/vendor/voyager/formfields/relationship.blade.php
这样 voyager 将在您的项目 View 中使用此文件,而不是供应商中的包文件。
第 2 步:
在 relationship.blade.php
的第 25 行中,您将看到以下代码块:
<select
class="form-control select2-ajax" name="{{ $options->column }}"
data-get-items-route="{{route('voyager.' . $dataType->slug.'.relation')}}"
data-get-items-field="{{$row->field}}"
>
@php
$model = app($options->model);
$query = $model::where($options->key, $dataTypeContent->{$options->column})->get();
@endphp
@if(!$row->required)
<option value="">{{__('voyager::generic.none')}}</option>
@endif
@foreach($query as $relationshipData)
<option value="{{ $relationshipData->{$options->key} }}" @if($dataTypeContent->{$options->column} == $relationshipData->{$options->key}){{ 'selected="selected"' }}@endif>{{ $relationshipData->{$options->label} }}</option>
@endforeach
</select>
您应该用以下内容覆盖它:
@if($options->table == 'categories')
<div class="ap-form-input">
@php
$model = app($options->model);
$query = $model::where('active', true)->get();
@endphp
<select class="form-control select2" id="language-select" name="{{ $options->column }}">
@if(!$row->required)
<option value="">{{__('voyager::generic.none')}}</option>
@endif
@foreach($query as $relationshipData)
<option value={{ $relationshipData->{$options->key} }} @if($dataTypeContent->{$options->column} == $relationshipData->{$options->key}){{ 'selected="selected"' }}@endif>{{ $relationshipData->{$options->label} }}</option>
@endforeach
</select>
</div>
@else
<div class="ap-form-input">
<select class="form-control select2" name="{{ $options->column }}">
@php
$model = app($options->model);
$query = $model::all();
@endphp
@if(!$row->required)
<option value="">{{__('voyager::generic.none')}}</option>
@endif
@foreach($query as $relationshipData)
<option value={{ $relationshipData->{$options->key} }} @if($dataTypeContent->{$options->column} == $relationshipData->{$options->key}){{ 'selected="selected"' }}@endif>{{ $relationshipData->{$options->label} }}</option>
@endforeach
</select>
</div>
@endif
这样,当您使用类别关系时,您将只获得事件类别。
关于laravel - 如何在 Laravel Voyager 中构建下拉选项的条件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57522958/