You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel分页中集成无日志用户筛选的if条件?

如何将无日志用户的筛选逻辑集成到Laravel分页查询中

你当前需要实现仅获取无日志记录的用户档案,现有Laravel控制器和视图代码如下:

现有控制器代码

public function not_checked_yet() {
    $pageName = "Not Checked Profiles";
    $status = "Active";
    $treeView = "Clients";
    $groupbyActivation = $this->groupbyActivation();
    $adminSerchs = BbUser::select(['id', 'profile_id', 'user_profession', 'user_looking_for', 'user_profile_image', 'user_date_of_birth', 'user_religion', 'present_district', 'country_of_residence', 'activation', 'membership_status'])
                    ->where('activation', 1)
                    ->where('membership_status', 107)
                    ->where('office_id', auth()->user()->agent_id)
                    ->orderBy('created_at', 'DESC')
                    ->paginate(5);  
    return view('agent.profile.search-result.not-check-search-result', compact('adminSerchs', 'pageName', 'groupbyActivation', 'status','treeView'));
}

现有视图筛选代码

@foreach ($adminSerchs as $key => $search_result)
   @php
     $none_check_log = \App\AgentsRemark::where('profile_id', 
     $search_result->profile_id)->count();
   @endphp
   @if($none_check_log < 1)
     <tr class="odd gradeX">
      <td>
       {{ $search_result->name }}
      </td>
   @endif
@endforeach
{{ $adminSerchs->links() }}

解决方案

可以利用Laravel的关联查询方法whereDoesntHave,直接在控制器的分页查询中筛选出无日志记录的用户,既避免视图中循环查询数据库的性能问题,又保证分页数据的准确性。

步骤1:在BbUser模型中定义关联

首先确保BbUser与AgentsRemark的关联关系已定义:

// app/Models/BbUser.php
public function agentsRemarks()
{
    // 通过profile_id字段关联AgentsRemark模型
    return $this->hasMany(\App\AgentsRemark::class, 'profile_id', 'profile_id');
}

步骤2:修改控制器查询逻辑

在原有查询基础上新增whereDoesntHave方法,直接过滤掉已有日志记录的用户:

public function not_checked_yet() {
    $pageName = "Not Checked Profiles";
    $status = "Active";
    $treeView = "Clients";
    $groupbyActivation = $this->groupbyActivation();
    $adminSerchs = BbUser::select(['id', 'profile_id', 'user_profession', 'user_looking_for', 'user_profile_image', 'user_date_of_birth', 'user_religion', 'present_district', 'country_of_residence', 'activation', 'membership_status'])
                    ->where('activation', 1)
                    ->where('membership_status', 107)
                    ->where('office_id', auth()->user()->agent_id)
                    // 新增:筛选无对应AgentsRemark记录的用户
                    ->whereDoesntHave('agentsRemarks')
                    ->orderBy('created_at', 'DESC')
                    ->paginate(5);  
    return view('agent.profile.search-result.not-check-search-result', compact('adminSerchs', 'pageName', 'groupbyActivation', 'status','treeView'));
}

步骤3:简化视图代码

此时分页返回的已经是过滤后的结果,视图中无需再做二次筛选,直接遍历即可:

@foreach ($adminSerchs as $key => $search_result)
     <tr class="odd gradeX">
      <td>
       {{ $search_result->name }}
      </td>
@endforeach
{{ $adminSerchs->links() }}

内容的提问来源于stack exchange,提问作者yasin konok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 15:17:46