[英]Search column of JSON data in PostgreSQL table
I need to store some JSON data in PostgreSQL table to use it as dynamic route generator. 我需要在PostgreSQL表中存储一些JSON数据,以将其用作动态路由生成器。 The migration is simple:
迁移很简单:
Schema::create($this->tablename, function (Blueprint $table)
{
$table->increments('id');
$table->string("uri", 123);
$table->json("middleware")->nullable();
$table->string("as", 123)->nullable();
}
I store the data this way: 我以这种方式存储数据:
$a = new Route();
$a->uri = "/test1";
$a->middleware=json_encode(["auth","web"]);
$a->as = "TestController@test";
$a->save();
So let's say that I need to filter all the routes that have auth
middleware. 假设我需要过滤所有具有
auth
中间件的路由。 How can I do it? 我该怎么做?
When I try 当我尝试
Route::where('middleware', 'AS', 'auth')->get();
...I get an error. ...我得到一个错误。 Is it possible to use it like that?
可以这样使用吗?
I use Laravel 5.2 and PostgreSQL 9.3.12 . 我使用Laravel 5.2和PostgreSQL 9.3.12 。
If you are using PostgreSQL 9.4 or later and have Laravel framework with version bigger than 5.2.29 you can use this syntax: 如果您使用的是PostgreSQL 9.4或更高版本,并且具有版本大于5.2.29的 Laravel框架,则可以使用以下语法:
Route::where('middleware','@>','"auth"')->get();
Change where to whereRaw and query for json field 更改到whereRaw的位置并查询json字段
Route::whereRaw("middleware->> 'auth'")->get();
Or 要么
Route::whereRaw("middleware::json->> 'auth'")->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.