用于根据id数组检索多行的Laravel Eloquent ORM方法

时间:2017-04-04 13:19:41

标签: php mysql laravel laravel-5 laravel-5.3

我可能没有按照我想要的方式提出问题,但这是我的困境:

我有一个名为'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”等于policy表中的“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中调用第131行/Database/Query/Grammars/Grammar.php

2 个答案:

答案 0 :(得分:1)

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

请参阅此处以了解如何定义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

答案 1 :(得分:0)

这似乎可以解决问题:

/*groups the expiring policies for each user*/
$with_expiring = $expiring->groupBy('client_id');

/*counts the number of clients with expiring policies instead of
 *counting the number of expiring policies first then checking
 *against a list of 'client_id's
 */
$total_with_expiring = $with_expiring->count();

/*groups the expired policies for each user*/
$with_expired = $expired->groupBy('client_id');

/*counts the number of clients with expired policies instead of
 *counting the number of expired policies first then checking
 *against a list of 'client_id's
 */
$total_with_expired = $with_expired->count();

还有确保&#39; client_id&#39;真正符合&#39; id&#39;在客户表中。