【发布时间】:2021-08-25 04:48:37
【问题描述】:
我的下拉菜单需要帮助。目前下拉菜单显示数据库中的所有选项。但是,我希望它仅基于用户 ID 显示选项。例如,此表单用于Urban Seafood,假设下拉菜单将仅显示Urban Seafood 选项,而不是显示所有选项。下面是控制器和视图刀片。谢谢。
调节控制器:
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>
【问题讨论】:
-
不是
Restorant::where(['active' => 1])->where('user_id', Auth::id())->get();吗? -
尝试了您的解决方案并收到此错误 ->
Undefined type 'Modules\Ingredient\Models\Auth'. -
你需要
use Illuminate\Support\Facades\Auth; -
哦,是的,它有效!谢谢! :)
-
不客气! :)
标签: php laravel select laravel-blade