首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据数据库中条目的状态进行查询和计数?

如何根据数据库中条目的状态进行查询和计数?
EN

Stack Overflow用户
提问于 2019-03-07 14:18:28
回答 2查看 74关注 0票数 0

我有一张名叫Patents的桌子。表值为:

代码语言:javascript
复制
 id | person_id | status | date_submitted | date_approved 

person_id(fk来自person表)包含创建该专利的人的id。Status有2个值,它们是1(待定)或2(已批准)。我现在面临的问题是,我不能同时计算被批准的专利和发明专利的人的总专利。

控制器文件

代码语言:javascript
复制
$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'));

叶片视图

代码语言:javascript
复制
<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变量只是确定这两个日期之间专利的日期。

EN

回答 2

Stack Overflow用户

发布于 2019-03-07 14:37:29

我可以看到控制器中有两个查询,每个查询生成一个集合。

然后在刀片文件中,您正在循环一个集合(而不是两个)。

您可能需要查看下面的控制器,其中提供了用于连接这些数据集的逻辑片段。

尝试在逻辑的不同点使用dd($myCollection)来查看数据,然后再将数据推送到控制器,以确保得到了预期的结果。

===============================================

编辑..。我在这里加入了一些代码。这应该能满足你的需要。也许有一个更好的方法来做收集,我不知道这将如何与大量的记录规模。

这应该能让你跑到现在为止。

代码语言:javascript
复制
//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
]
票数 0
EN

Stack Overflow用户

发布于 2019-03-07 14:50:00

如果您使用的是laravel,您可以使用以下内容:

代码语言:javascript
复制
$zzzz = Model::count();

然后在你的刀刃上你只需要打电话

代码语言:javascript
复制
{{$zzzz}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55045978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档