laravel 分类的列表查询
public function index(Request $request, ResponseFactoryContract $response, QuestionModel $questionModel)
{
$userID = $request->user('api')->id ?? 0;
$limit = max(1, min(30, $request->query('limit', 15)));
$offset = max(0, $request->query('offset', 0));
$subject = $request->query('subject');
$map = [
'all' => function ($query) {
$query->orderBy('id', 'desc');
},
'new' => function ($query) {
$query->where('answers_count', 0)
->orderBy('id', 'desc');
},
'hot' => function ($query) use ($questionModel) {
$query->whereBetween('created_at', [
$questionModel->freshTimestamp()->subMonth(1),
$questionModel->freshTimestamp(),
])->where('answers_count', '!=', 0);
$query->orderBy('answers_count', 'desc');
},
'reward' => function ($query) {
$query->where('amount', '!=', 0)
->orderBy('id', 'desc');
},
'excellent' => function ($query) {
$query->where('excellent', '!=', 0)
->orderBy('id', 'desc');
},
'follow' => function ($query) use ($userID) {
$query->whereExists(function ($query) use ($userID) {
$query->from('question_watcher')
->where('question_watcher.user_id', '=', $userID)
->whereRaw('question_watcher.question_id = questions.id');
});
},
];
$type = in_array($type = $request->query('type', 'new'), array_keys($map)) ? $type : 'new';
call_user_func($map[$type], $query = $questionModel
->whereDoesntHave('blacks', function ($query) use ($userID) {
$query->where('user_id', $userID);
})
->when($subject, function ($query) use ($subject) {
return $query->where('subject', 'like', '%'.$subject.'%');
})
->limit($limit)
->offset($offset));
$questions = $query->get();
$questions->load('user'); return $response->json($questions->map(function (QuestionModel $question) use ($userID) {
if ($question->anonymity && $question->user_id !== $userID) {
$question->addHidden('user');
$question->user_id = 0;
} $question->answer = $question->answers()
->whereDoesntHave('blacks', function ($query) use ($userID) {
$query->where('user_id', $userID);
})
->with('user')
->orderBy('id', 'desc')
->first(); if ($question->answer) {
if ($question->answer->anonymity && $question->answer->user_id !== $userID) {
$question->answer->addHidden('user');
$question->answer->user_id = 0;
}
$question->answer->liked = (bool) $question->answer->liked($userID);
$question->answer->collected = (bool) $question->answer->collected($userID);
$question->answer->rewarded = (bool) $question->answer->rewarders()->where('user_id', $userID)->first();
$question->look && $question->answer->could = true; if ($question->look && $question->answer->invited && (! $question->answer->onlookers()->where('user_id', $userID)->first()) && $question->answer->user_id !== $userID && $question->user_id !== $userID) {
$question->answer->could = false;
$question->answer->body = null;
}
} return $question;
}))->setStatusCode(200);
}
laravel 分类的列表查询的更多相关文章
- Laravel Query Builder 复杂查询案例:子查询实现分区查询 partition by
案例 案例:Laravel 在文章列表中附带上前10条评论?,在获取文章列表时同时把每个文章的前10条评论一同查询出来. 这是典型分区查询案例,需要根据 comments 表中的 post_id 字段 ...
- SHAREPOINT - CAML列表查询
首先要了解的是CAML(Collaboration Application Markup Language)不仅仅是用在对列表.文档库的查询,字段的定义,站点定义等处处使用的都是CAML. 简单的提一 ...
- atitit.提升开发效率---MDA 软件开发方式的革命(5)----列表查询建模
)----列表查询建模 1. 配置条件字段@Conditional 1 2. 配置条件字段显示类型为range----@Conditional(displayType = displayType.ra ...
- 【Javascript】列表查询页面,简单地保存上一次查询的查询参数
开发中经常做一些查询参数 + 列表参数的功能,这些功能有时候需提供导出Excel,或带超链接到其他明细页面的功能点. 在一些交互性要求严格的系统,需求方会要求: 用户第一个输入某些查询条件进行列表查询 ...
- Moss列表查询,删除条目,更新条目。
基于Query语句的列表查询 function retrieveListItems(itemId) { var siteUrl=_spPageContextInfo.webServerRelat ...
- 【SSH系列】一步步深入springmvc+商品列表查询demo
在前面的博文中,小编主要简单的介绍springmvc的体系结构.mvc模式的优缺点以及mvc框架,今天我们来继续学习springmvc的相关知识,在这篇博文中,小编讲解过springmvc的体系结构, ...
- SharePoint中跨列表查询
1,最近的项目中遇到一个需求,站点中有几十个列表,其中每5,6个列表属于一个模块下的.客户的需求是,首页上显示一个模块下所有列表数据的前5条,并按创建时间排序. 2,刚刚考虑到这块的实现方法时,用的是 ...
- 写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我
写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我 1.HttpUtil工具类,用于模拟用户登录以及爬取网页: using System; using ...
- Office365学习笔记—列表查询,删除条目,更新条目。
1,基于Query语句的列表查询. function retrieveListItems(itemId) { var siteUrl=_spPageContextInfo.webServerRelat ...
随机推荐
- vue学习之生命周期和钩子函数
参考文章:Vue2.0 探索之路——生命周期和钩子函数的一些理解 抛出问题: 我们有时候会在几个钩子函数里做一些事情,那么什么时候做,该在哪个函数里做? 生命周期简介 结合代码看el 和 data以及 ...
- python,中使用while...else 和 for...else 还有try...else,另外就是运用with关键字
其他语言中else只可以和if进行组合,也就是我们常见的if...else,但是python为else赋予了新的声明.它可以和while .for .try一起串联使用. 下面我们介绍和while串联 ...
- C/C++ assert()函数用法总结
1. 简介 assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行. 原型定义: #include <assert.h>void ass ...
- 【Linux】CentOS7.0下安装JDK环境
写在前面:此次试验是在CentOS7上面安装的,亲测成功. 所需工具:JDK1.8安装包,xftp 具体步骤: 1,首先使用xftp连接到自己的虚拟机,然后查看是否有"/usr/java/j ...
- 使用thrift实现订阅服务和发布服务
使用thrift实现订阅服务和发布服务 服务:订阅服务 market_subscriber 和 发布服务 market_publisher功能:market_subscriber 能够向 market ...
- genPanel.py
#!/usr/bin/python # -*- coding: UTF-8 -*- import os import sys import re import shutil import glob ' ...
- hibernate框架学习之数据查询(QBC)
lQBC(Query By Criteria)是一种Hibernate中使用面向对象的格式进行查询的计数 lQBC查询方式步骤 •获取Session对象 •初始化Criteria对象(使用Sessio ...
- 007grafana监控时间戳转换
一. https://d.jyall.me/dashboard-solo/db/soloview?panelId=1&var-metrics=stats.gauges.zookeeper.mo ...
- 1-HTML Attributes
下表列举了常用的Html属性 Attribute Description alt Specifies an alternative text for an image, when the image ...
- Python Redis pipeline操作
Redis是建立在TCP协议基础上的CS架构,客户端client对redis server采取请求响应的方式交互. 一般来说客户端从提交请求到得到服务器相应,需要传送两个tcp报文. 设想这样的一个场 ...