dongqishun6409 2015-07-16 23:16
浏览 36
已采纳

Laravel链接范围查询

I have two scope queries for my DB.

The data I query looks like this:

   {
       year: "2015",
       term: "Summer",
       subject_code: "DIGM",
       course_no: "350",
       instr_type: "Lab",
       instr_method: "Face To Face",
       section: "003",
       crn: "42953",
       course_title: "Digital Storytelling",
       credits: "3.0",
       day: "R",
       time: "06:30 pm - 09:20 pm",
       instructor: "Teacher Name",
       campus: "University Building",
       max_enroll: "18",
       enroll: "18",
       building: "PLACE",
       room: null,
       description: "By surfing the internet and playing computer games, by lectures, assigned readings, class screening, and research projects, this class explores the impact of digital media on art, design and daily living. This is a writing intensive course. ",
       pre_reqs: "",
       co_reqs: ""
   }

search simply looks for items based on user input, so like the name of the course, or its instructors

/**
 * Search for course title or subject name
 * @param $query
 * @param $searchTerm Course Title or Subject Name i.e. "ECEC 355" or
 *                    "Digital Logic"
 * @return mixed
 */
public function scopeSearch($query, $searchTerm) {
    return $query
        ->where('course_title', 'like', '%' . $searchTerm . '%')
        ->orWhere(DB::raw("subject_code || ' ' ||  course_no"),
            'like',
            '%' . $searchTerm . '%'
        )
        ->orWhere('instructor', 'like', '%' . $searchTerm . '%')
        ;
}

The following scope queries returns simply the lectures. I tried this query like this: $class->lectures()->get() and it works - it pulls all the lectures.

public function scopeLectures($query) {
    return $query
        ->where('instr_type', 'like', LECTURE) // where LECTURE is a constant
        ;
}

However, if I chain my scope queries together:

$class::search('digm 350')->lecture()->get();

I would get all the results of search instead of search->lab.

Not quite sure why.

  • 写回答

1条回答 默认 最新

  • doujie4344 2015-07-16 23:31
    关注

    The reason is the ORs that you do in search. When you apply both scopes the query that is generated is:

    where `course_title` like ? or subject_code || ' ' ||  course_no like ? or `instructor` like ? and `instr_type` like ?
    

    while what you need is

    where (`course_title` like ? or subject_code || ' ' ||  course_no like ? or `instructor` like ? and `instr_type` like ?)
    

    Notice that the constraints from first scope have been put in parentheses.

    You need to change the way you apply constraints in your search scope:

    public function scopeSearch($query, $searchTerm) {
      return $query->where(function($query) use ($searchTerm) {
        $query
          ->where('course_title', 'like', '%' . $searchTerm . '%')
          ->orWhere(DB::raw("subject_code || ' ' ||  course_no"), 'like', '%' . $searchTerm . '%')
          ->orWhere('instructor', 'like', '%' . $searchTerm . '%');
     });
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值