【问题标题】:Show dropdown option based on user id根据用户 ID 显示下拉选项
【发布时间】: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' =&gt; 1])-&gt;where('user_id', Auth::id())-&gt;get();吗?
  • 尝试了您的解决方案并收到此错误 -> Undefined type 'Modules\Ingredient\Models\Auth'.
  • 你需要use Illuminate\Support\Facades\Auth;
  • 哦,是的,它有效!谢谢! :)
  • 不客气! :)

标签: php laravel select laravel-blade


【解决方案1】:

你需要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'));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    相关资源
    最近更新 更多