dongtu7567 2018-01-03 03:32
浏览 47
已采纳

使用数据透视表过滤查询

I just want to ask on how to filter my query with a pivot table. This is how my tables are

users(id,name)

companies(id,name)

users_companies(id,user_id,company_id)

I really want to know how can i filter my list using drop down with pivot table Then this is how it goes to my EloquentUser

   public function paginate($perPage, $search = null, $status = null, $emp_status = null, $level = null, $age = null, $gender = null, $civil_status = null, $role_id = null, $birthmonth = null, $company = null)
    {
    $query = User::query();

    if($company && $company != "" && $company != "All" ) {
            $query->where(function ($q) use ($company) {
                $q->where('id', '=', $company);
            });
        }

This is for my list.blade or where my dropdown is located

<div class="form-group-sm"> 
                <select class="form-control form-control-sm" id="company" name="company">
                    <option selected  disabled>Company</option>
                    @foreach ($companies as $company)
                        @if (($company->id)=="1")
                        <option>All</option>
                        @else
                        <option value="{{ $company->id }}">{{ $company->name }}</option>
                        @endif
                    @endforeach
                </select>
            </div>

Thank you so much experts!!!

  • 写回答

2条回答 默认 最新

  • drtoaamk20278 2018-01-08 09:51
    关注

    You can get users by using whereHas method, here is the example

    $selected_company_id = $company; //this is form input, company which you selected
    
    $users = User::whereHas('companies', function ($query) use($selected_company_id) {
        $query->where('company_id', $selected_company_id);
    })->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元