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 分类的列表查询的更多相关文章

  1. Laravel Query Builder 复杂查询案例:子查询实现分区查询 partition by

    案例 案例:Laravel 在文章列表中附带上前10条评论?,在获取文章列表时同时把每个文章的前10条评论一同查询出来. 这是典型分区查询案例,需要根据 comments 表中的 post_id 字段 ...

  2. SHAREPOINT - CAML列表查询

    首先要了解的是CAML(Collaboration Application Markup Language)不仅仅是用在对列表.文档库的查询,字段的定义,站点定义等处处使用的都是CAML. 简单的提一 ...

  3. atitit.提升开发效率---MDA 软件开发方式的革命(5)----列表查询建模

    )----列表查询建模 1. 配置条件字段@Conditional 1 2. 配置条件字段显示类型为range----@Conditional(displayType = displayType.ra ...

  4. 【Javascript】列表查询页面,简单地保存上一次查询的查询参数

    开发中经常做一些查询参数 + 列表参数的功能,这些功能有时候需提供导出Excel,或带超链接到其他明细页面的功能点. 在一些交互性要求严格的系统,需求方会要求: 用户第一个输入某些查询条件进行列表查询 ...

  5. Moss列表查询,删除条目,更新条目。

    基于Query语句的列表查询 function retrieveListItems(itemId) {    var siteUrl=_spPageContextInfo.webServerRelat ...

  6. 【SSH系列】一步步深入springmvc+商品列表查询demo

    在前面的博文中,小编主要简单的介绍springmvc的体系结构.mvc模式的优缺点以及mvc框架,今天我们来继续学习springmvc的相关知识,在这篇博文中,小编讲解过springmvc的体系结构, ...

  7. SharePoint中跨列表查询

    1,最近的项目中遇到一个需求,站点中有几十个列表,其中每5,6个列表属于一个模块下的.客户的需求是,首页上显示一个模块下所有列表数据的前5条,并按创建时间排序. 2,刚刚考虑到这块的实现方法时,用的是 ...

  8. 写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我

    写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我 1.HttpUtil工具类,用于模拟用户登录以及爬取网页: using System; using ...

  9. Office365学习笔记—列表查询,删除条目,更新条目。

    1,基于Query语句的列表查询. function retrieveListItems(itemId) { var siteUrl=_spPageContextInfo.webServerRelat ...

随机推荐

  1. LOJ #2587「APIO2018」铁人两项

    是不是$ vector$存图非常慢啊...... 题意:求数对$(x,y,z)$的数量使得存在一条$x$到$z$的路径上经过$y$,要求$x,y,z$两两不同  LOJ #2587 $ Solutio ...

  2. js 关键字 in 的使用方法

    参考地址:http://www.cnblogs.com/qiantuwuliang/archive/2011/01/08/1930643.html in 操作符用于确定某个元素是否在数组中,判断某个属 ...

  3. Android中高级工程师面试题

    https://www.cnblogs.com/huangjialin/p/8657565.html(存在不少答案错误,可参照知识点复习,答案不可全信)  上 https://www.cnblogs. ...

  4. ASP.NET MVC - XML节点查找

    Jquery操作Xml Jquery1.2开始不再支持XPath表达式. <?xml version="1.0" encoding="utf-8" ?&g ...

  5. 【转载】TensorFlow学习笔记:共享变量

    原文链接:http://jermmy.xyz/2017/08/25/2017-8-25-learn-tensorflow-shared-variables/ 本文是根据 TensorFlow 官方教程 ...

  6. OVS-----CentOS7.2上安装OVS软件

    1.安装依赖包: yum -y install make gcc openssl-devel autoconf automake rpm-build redhat-rpm-config yum -y ...

  7. uniGUI -- Web 应用框架 Delphi + Ext JS

    uniGUI --  Web 应用框架 ,基于 Delphi + Ext JS  技术.

  8. 使用cstdiofile在vs2010中无法写入中文的问题

    在VC2010环境下, 以下代码无法实现使用CStdioFile向文本文件中写入中文(用notepad.exe查看不到写入的中文) CStdioFile file; file.Open(…); fil ...

  9. nodejs+express+mongodb简单的例子

    简单的介绍下node+express+mongodb这三个东西.node:是运行在服务器端的程序语言,表面上看过去就是javascript一样的东西,但是呢,确实就是服务器语言,个人觉得在一定层次上比 ...

  10. 【转】浅谈Java中的hashcode方法

    哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: public native int hashCode(); 根据这个 ...