我可能没有按照我想要的方式表达这个问题,但这是我的Dilema:

我有一个名为“ 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>





“政策”表中的“ client_id”与“客户”表中的“ 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/中调用第131行的Query / Grammars / Grammar.php

最佳答案

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 Facades编写查询。

请参阅此处以了解如何定义口才模型https://laravel.com/docs/5.4/eloquent#defining-models

定义Client雄辩的模型后,您可以通过以下方式检索所有客户端:

$clients = Client::whereIn('id', [1, 2, 3]); // where 1,2,3 is the client ids

关于php - Laravel Eloquent ORM方法,用于基于ID数组检索多行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43208457/

10-11 12:10