[英]How to build conditional list of dropdown options in Laravel Voyager
我對Laravel Voyager v1.2有問題。 我需要根據條件輕松選擇下拉列表中相關選項列表的列表。 假設我有與類別相關的帖子,但我不想在選擇下拉列表中列出所有類別,而只列出活動的類別。
public function category_id() {
return $this->belongsTo('Post::class')->where('active',1);
}
{
"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中的刀片輕松地做到這一點:
復制relationship.blade.php
來自:/“您的項目” /"YourProject"/vendor/tcg/voyager/resources/views/formfields/relationship.blade.php
若要:/“您的項目” /"YourProject"/resources/views/vendor/voyager/formfields/relationship.blade.php
這樣,voyager將在您的項目視圖中使用此文件,而不是供應商中的軟件包文件。
閱讀更多: Voyager文檔
在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
這樣,當您使用類別關系時,您只會獲得活動類別。
單擊鏈接並按照以下簡單步驟操作,剛才我解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.