public function indexQuestions(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'); //问题主题
$topic_id = $request->query('topic_id'); //话题id
if(!$topic_id){
return response()->json(['message' => ['话题id必须给出']], 422);
}
//话题关联问题id
$questions_id=QuestionTopic::where('topic_id',$topic_id)->pluck('question_id');
if(!$questions_id){
return response()->json([], 200);
}else{
$questions_id=$questions_id->toArray();
} $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', 'all'), array_keys($map)) ? $type : 'all';
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.'%');
})
->whereIn('id',$questions_id)
->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. 在Bootstrap开发框架中使用bootstrapTable表格插件和jstree树形列表插件时候,对树列表条件和查询条件的处理

    在我Boostrap框架中,很多地方需要使用bootstrapTable表格插件和jstree树形列表插件来共同构建一个比较常见的查询界面,bootstrapTable表格插件主要用来实现数据的分页和 ...

  2. 列表查询组件代码, 简化拼接条件SQL语句的麻烦

    列表查询组件代码, 简化拼接条件SQL语句的麻烦 多条件查询

  3. laravel多条件模糊查询

    1.运用cmd在项目根目录下创建路由组 php artisan make:controller queryController --resource 1.1数据库信息(student) CREATE ...

  4. Oracle 检索数据(查询数据、select语句)

    用户对表或视图最常进行的操作就是检索数据,检索数据可以通过 select 语句来实现,该语句由多个子句组成,通过这些子句完成筛选.投影和连接等各种数据操作,最终得到想要的结果. 语法: select ...

  5. asp.net mvc多条件+分页查询解决方案

    开发环境vs2010 css:bootstrap js:jquery bootstrap paginator 原先只是想做个mvc的分页,但是一般的数据展现都需要检索条件,而且是多个条件,所以就变成了 ...

  6. JSP+Servlet+javabean+oracle实现页面多条件模糊查询

    之前写过一篇JSP+Servlet+javabean+mysql实现页面多条件模糊查询 使用的是mysql进行的分页查询,mysql用limit控制,而oracle则是用rownum,今天第一次写or ...

  7. SHAREPOINT - CAML列表查询

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

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

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

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

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

随机推荐

  1. 特性Attribute

    1.简介 特性(attribute)是被指定给某一声明的一则附加的声明性信息. 在C#中,有一个小的预定义特性集合.在学习如何建立我们自己的定制特性(custom attributes)之前,我们先来 ...

  2. Kotlin中三元运算符

    int a = 10;int b = 11;int c = a > b ? a : b; 到了 kotlin中 val a = 10val b = 11val c = if (a > b) ...

  3. Eclipse安装教程 ——史上最详细安装Java &Python教程说明

    参考链接:https://blog.csdn.net/zichen_ziqi/article/details/73995755

  4. SpringCloud知识点20190313

    1.SpringBoot和SpringCloud的关系(面试题) Spring Boot 可以离开 Spring Cloud 单独使用开发项目,但是Spring Cloud离不开SpringBoot, ...

  5. MySQL创建数据库指定字符集

    CREATE DATABASE IF NOT EXISTS dbname DEFAULT CHARSET utf8;

  6. java String、StringBuffer、StringBuild、StringTokenizer

    StringBuffer线程安全.StringBuilder线程不安全 效率: StringBuilder(线程不安全) > StringBuffer(线程安全) > String Str ...

  7. Python运维开发基础07-文件基础【转】

    一,文件的基础操作 对文件操作的流程 [x] :打开文件,得到文件句柄并赋值给一个变量 [x] :通过句柄对文件进行操作 [x] :关闭文件 创建初始操作模板文件 [root@localhost sc ...

  8. 【OpenCV】SIFT原理与源码分析:DoG尺度空间构造

    原文地址:http://blog.csdn.net/xiaowei_cqu/article/details/8067881 尺度空间理论   自然界中的物体随着观测尺度不同有不同的表现形态.例如我们形 ...

  9. web@css高级选择器(after,befor用法),基本css样式

    1.高阶选择器:子代后代,相邻通用兄弟,交集并集,属性,伪类,伪元素子代后代选择器 div>p{}  div p{}相邻通用兄弟 div+p{}  div~p{}理解:div同学的同桌p  di ...

  10. virtual box 安装centos min

    2018-4-19 22:20:40 星期四 之前不小心把用了很久的centos镜像删掉了.....这里记录下安装最小版centos的步骤 1. 安装centos 2. 开启网络, 并设置为随机启动 ...