我有一张名叫Patents的桌子。表值为:
id | person_id | status | date_submitted | date_approved person_id(fk来自person表)包含创建该专利的人的id。Status有2个值,它们是1(待定)或2(已批准)。我现在面临的问题是,我不能同时计算被批准的专利和发明专利的人的总专利。
控制器文件
$start = $request->input('start');
$end = $request->input('end');
$approvedPatents = DB::table('patents')
->select('person_id', DB::raw('count(*) as CountofApprovedPatents, status'))
->where([
['status', '=', 2],
['date_approved', '>=', $start],
['date_approved', '<=', $end],
])
->groupBy('person_id')
->orderBy('status', 'desc')
->get();
$totalPatents = DB::table('patents')
->select('person_id', DB::raw('count(*) as CountofPatents'))
->where([
['date_approved', '>=', $start],
['date_approved', '<=', $end],
])
->groupBy('person_id')
->orderBy('status', 'desc')
->get();
$patents = $approvedPatents->merge($totalPatents);
$results = $patents->all();
return view('admin.patents.showPatents', compact('results',
'start', 'end'));叶片视图
<thead>
<tr>
<th>Person ID</th>
<th>Patents Approved</th>
<th>Total Patents</th>
</tr>
</thead>
<tbody>
@foreach($results as $result)
<tr>
<td>{{ $result->person_id }} </td>
<td>{{ $result->CountofApprovedPatents }} </td> //Error
<td>{{ $result->CountofPatents }} </td> //Error
</tr>
@endforeach
</tbody>我可以显示$patent->CountofApprovedPatents,但不能显示其他选择。我真的需要帮助,我不知道如何得到CountofPatents,因为我有两个独立的选择。我试着为每个变量做一个预测,但是它会显示双倍的结果,而不是在一行中编译。$start和$end变量只是确定这两个日期之间专利的日期。
发布于 2019-03-07 14:37:29
我可以看到控制器中有两个查询,每个查询生成一个集合。
然后在刀片文件中,您正在循环一个集合(而不是两个)。
您可能需要查看下面的控制器,其中提供了用于连接这些数据集的逻辑片段。
尝试在逻辑的不同点使用dd($myCollection)来查看数据,然后再将数据推送到控制器,以确保得到了预期的结果。
===============================================
编辑..。我在这里加入了一些代码。这应该能满足你的需要。也许有一个更好的方法来做收集,我不知道这将如何与大量的记录规模。
这应该能让你跑到现在为止。
//setup test
$approvedPatents = collect([
['name'=>'andy', 'count'=> 1],
['name'=>'joe', 'count'=> 2],
['name'=>'william', 'count'=> 5],
]);
$allPatents = collect([
['name'=>'andy', 'count'=> 11],
['name'=>'joe', 'count'=> 12],
['name'=>'william', 'count'=> 15],
]);
//combine collections - basically scan the allpatents collection and get the count from the matching currentpatents collection, and create a new collection entry with both in the combinedpatents collection
$combined = collect();
foreach ($allPatents as $patent) {
$approvedPatentCount = ($approvedPatents->where('name',$patent['name'])->first())['count'];
$combined->push([
'name'=>$patent['name'],
'allPatentsCount'=>$patent['count'],
'approvedPatentsCount'=>$approvedPatentCount,
]);
}
//display results
foreach ($combined as $c) {
dump($c);
}
// results from browser
array:3 [▼
"name" => "andy"
"allPatentsCount" => 11
"approvedPatentsCount" => 1
]
array:3 [▼
"name" => "joe"
"allPatentsCount" => 12
"approvedPatentsCount" => 2
]
array:3 [▼
"name" => "william"
"allPatentsCount" => 15
"approvedPatentsCount" => 5
]发布于 2019-03-07 14:50:00
如果您使用的是laravel,您可以使用以下内容:
$zzzz = Model::count();然后在你的刀刃上你只需要打电话
{{$zzzz}}https://stackoverflow.com/questions/55045978
复制相似问题