laravel Eloquent 如何实现 FIND_IN_SET ,并实现分页

2017-12-09 11:18:13

首先介绍find_in_set

有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 

现在有篇文章他既是 头条,又是热点,还是图文, 

type中以 1,3,4的格式存储. 

们我们如何用sql查找所有type中有4图文标准的文章呢?? 


这就要我们的find_in_set出马的时候到了. 

以下为引用的内容: 

select * from article where FIND_IN_SET('4',type) 


然后这个语法在laravle eloquent中怎么实现呢

  1. 从网上看到的网友回复是这样的



    down voteaccepted

    You need to escape the call to FIND_IN_SET() using quotes:

    $query = DB::table('tags_value')
             ->whereRaw('FIND_IN_SET(css,Tags)')
             ->get();

    If you want to parameterize the column for which you search in FIND_IN_SET, then you can try something like this:

    $colname = 'css'
    $query = DB::table('tags_value')
             ->whereRaw('FIND_IN_SET(?,Tags)', [$colname])
             ->get();
  2. 上面的语法我并没有成功下面是我成功的语法


$articles = DB::table("article")->whereRaw('FIND_IN_SET(1,types)',true)->where("status",1)->orderBy("id","desc")->get()->paginate(15);


    我的代码与上面的例子有出入,但是意思是一样的。

  • 2020-12-07 22:02:44

    intellij idea远程开发的几个想法

    我之前是用idea上面自带的stfp来做的本地开发同步到linux服务器编译,但是我发现这个如果多个客户端同时开发,或者多个同事一起开发,服务器上的就不能更新到本地。是不能增量更新到本地,必须全部下载,比对下载也行,但是工程量打了就特别慢。

  • 2020-12-07 22:06:13

    System Extension Blocked - warning

    After upgrading your macOS computer to High Sierra 10.13.4 or higher (starting in April 2018), you may see a message about a System Extension Blocked. At Williams we have seen this warning appear for these programs:

  • 2020-12-08 08:57:12

    win10上使用win-sshfs

    首先在GitHub上下载DokanSetup-1.0.5.1000和WinSSHFS-1.6.1.13-devel 注意:Dokan不能使用最新的版本,得使用1.0.5版本。要不win-sshfs会报Dokan版本错误的问题。(win10版本)

  • 2020-12-08 11:51:54

    Ubuntu安装Node.js和npm

    Node.js是基于Chrome的JavaScript构建的跨平台JavaScript运行时环境,npm是Node.js的默认程序包管理器,也是世界上最大的软件注册表。本篇文章展示了三种在Ubuntu 20.04服务器上安装Node.js和npm的方法。