根据用户 ID 显示下拉选项
Show dropdown option based on user id
我的下拉菜单需要帮助。目前,下拉列表显示数据库中的所有选项。但是,我希望它仅根据用户 ID 显示选项。例如,此表单用于 Urban Seafood
,假设下拉列表将仅显示 Urban Seafood
选项而不是显示所有选项。下面是控制器和视图 blade。谢谢。
调整控制器:
public function create() {
$restorants = Restorant::where(['active' => 1])->get();
return view('ingredient::adjustments.create', compact('restorants'));
}
查看:
<select class="form-control select2" name="restorant_id">
<option disabled selected value> -- {{ __('Select an option') }} -- </option>
@foreach ($restorants as $restorant)
<option <?php if(isset($_GET['restorant_id'])&&$_GET['restorant_id'].""==$restorant->id.""){echo "selected";} ?> value="{{ $restorant->id }}">{{$restorant->name}}</option>
@endforeach
</select>
你需要 Auth
facade
use Illuminate\Support\Facades\Auth;
// ...
public function create() {
$restorants = Restorant::where('active', 1)->where('user_id', Auth::id())->get();
return view('ingredient::adjustments.create', compact('restorants'));
}
我的下拉菜单需要帮助。目前,下拉列表显示数据库中的所有选项。但是,我希望它仅根据用户 ID 显示选项。例如,此表单用于 Urban Seafood
,假设下拉列表将仅显示 Urban Seafood
选项而不是显示所有选项。下面是控制器和视图 blade。谢谢。
调整控制器:
public function create() {
$restorants = Restorant::where(['active' => 1])->get();
return view('ingredient::adjustments.create', compact('restorants'));
}
查看:
<select class="form-control select2" name="restorant_id">
<option disabled selected value> -- {{ __('Select an option') }} -- </option>
@foreach ($restorants as $restorant)
<option <?php if(isset($_GET['restorant_id'])&&$_GET['restorant_id'].""==$restorant->id.""){echo "selected";} ?> value="{{ $restorant->id }}">{{$restorant->name}}</option>
@endforeach
</select>
你需要 Auth
facade
use Illuminate\Support\Facades\Auth;
// ...
public function create() {
$restorants = Restorant::where('active', 1)->where('user_id', Auth::id())->get();
return view('ingredient::adjustments.create', compact('restorants'));
}