问题描述
我的下拉菜单需要帮助。当前下拉列表显示数据库中的所有选项。但是,我希望它仅根据用户 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>
解决方法
您需要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'));
}