我可能没有按照我想要的方式表达这个问题,但这是我的困境:
我有一个名为“clients”的用户表,其结构如下:
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-yw4l{vertical-align:top}
</style>
<table class="tg">
<tr>
<th class="tg-yw4l">id</th>
<th class="tg-yw4l">int(10)</th>
<th class="tg-031e">unsigned</th>
</tr>
<tr>
<td class="tg-yw4l">client_name</td>
<td class="tg-yw4l">varchar(255)</td>
<td class="tg-yw4l"></td>
</tr>
<tr>
<td class="tg-yw4l">id_number</td>
<td class="tg-yw4l">int(11)</td>
<td class="tg-yw4l"></td>
</tr>
<tr>
<td class="tg-yw4l">email</td>
<td class="tg-yw4l">varchar(255)</td>
<td class="tg-yw4l"></td>
</tr>
</table>
我有另一个策略“策略”表,其结构如下:
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-yw4l{vertical-align:top}
</style>
<table class="tg">
<tr>
<th class="tg-yw4l">id</th>
<th class="tg-yw4l">int(10)</th>
<th class="tg-yw4l">unsigned</th>
<th class="tg-031e">AUTO INCREMENT</th>
</tr>
<tr>
<td class="tg-yw4l">client_id</td>
<td class="tg-yw4l">int(10)</td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
</tr>
<tr>
<td class="tg-yw4l">type_id</td>
<td class="tg-yw4l">int(11)</td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
</tr>
<tr>
<td class="tg-yw4l">product_id</td>
<td class="tg-yw4l">int(11)</td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
</tr>
</table>
“policies”表中的“client_id”相当于“clients”表中的“id”。一个客户可以有多个“策略”。当策略过期时,我想获取策略过期的客户端列表。其逻辑是并非每个用户都会有一个过期或即将到期的策略,但一个用户可以拥有多个即将到期或即将到期的策略。 (例如,在一张有 10 个客户端的表中,总共有 20 个即将到期或已过期的保单,则可以有 5 个客户端具有即将到期或已过期的保单等。)每个保单都与一个客户端相关联。该客户端可以拥有无限数量的策略
考虑到客户的“id”等于策略表中的“client_id”,我应该如何计算策略到期的客户数量?
这是我迄今为止尝试过的:
$today = Carbon::today()->toDateString();
$yesterday = Carbon::yesterday()->toDateString();
$expiry_period = Carbon::today()->addDays(3)->toDateString();
$client = DB::table('clients')->join('active_policies', 'clients.id', '=', 'active_policies.client_id')->select('clients.id');
$active_clients = Clients::all();
$policies = ActivePolicies::all();
$total_policies = $policies->count();
$expiring = $policies->where('renewal_date', '<=', $expiry_period)->where('renewal_date', '>=', $today);
$total_expiring = $expiring->count();
$expired = $policies->where('renewal_date', '<=', $yesterday);
$total_expired = $expired->count();
//here's where I try to count the clients with expiring policies.
$with_expiring = $expiring->where('client_id', '=', DB::table('clients')->get('clients.id'))->count();
//here's where I try to count the clients with expired policies.
$with_expired = $expired->where('client_id', '=', DB::table('clients')->get('clients.id'))->count();
执行时出现以下错误:
类型错误:传递给 Illuminate\Database\Grammar::columnize() 的参数 1 必须是给定的数组、字符串类型,在/var/.../app/vendor/laravel/framework/src/Illuminate 中调用/Database/Query/Grammars/Grammar.php 第 131 行
最佳答案
DB::table('clients')->get('clients.id')
返回Collection
而$expired->where('client_id', '=', '<this should be string>')
尝试:$expiring->where('client_id', '=', DB::table('clients')->get('clients.id')->first())->count()
但是该代码不会提供您想要的内容。
您应该创建 Eloquent 模型并使用它,而不是使用 DB
编写查询外墙。
请参阅此处了解如何定义 Eloquent 模型 https://laravel.com/docs/5.4/eloquent#defining-models
定义 Client
后Eloquent 模型,您可以通过以下方式检索所有客户端:
$clients = Client::whereIn('id', [1, 2, 3]); // where 1,2,3 is the client ids
关于php - Laravel Eloquent ORM 方法基于 ids 数组检索多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43208457/