2017-04-04 74 views
0

我可能沒有表述的問題,我想,但來這裏的路上檢索多行Laravel雄辯ORM方法是我的姿態的尷尬:基於ID的數組

我有一個名爲「客戶與用戶的表結構下:

<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」是「身份證」在「客戶」表的等價物。客戶可以有多個'政策'。當某個策略過期時,我想要獲取已過期策略的客戶列表。邏輯不是每個用戶都會有一個過期或過期的策略,但一個用戶可能有多個過期和過期的策略。 (例如,在10個客戶的表中,總共有20個到期或過期的策略,可能有5個客戶有過期或過期的策略等。)每個策略都與一個客戶相關聯。該客戶端可以有無限數量的策略

考慮到客戶端的'id'等於policy_table中的'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(); 

我得到在執行時出現以下錯誤:

類型錯誤:參數1傳遞給照亮\數據庫\語法:: columnize()必須是該類型的陣列,串給出的,稱爲在/var/.../app/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php線131上

回答

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(); 

仍然存在確保'client_id'真正匹配的問題s在客戶表中用'id'。

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編寫查詢。

看到這裏閱讀如何定義雄辯模式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

+0

感謝您澄清我在哪裏比較兩種不同類型。但是,在這段代碼中: $ clients = Client :: whereIn('id',[1,2,3]); //其中1,2,3是客戶端ID 如何爲所有具有過期或過期策略的客戶端創建'client_id'數組? 但是我有一個更簡單的方法來獲取和計算過期和過期策略的客戶端: $ with_expiring = $ expiring-> groupBy('client_id'); $ total_with_expiring = $ with_expiring-> count(); $ with_expired = $ expired-> groupBy('client_id'); $ total_with_expired = $ with_expired-> count(); –