2016-10-04 46 views
0

我創建的表如何在laravell中獲得postgress JSON類型的數據?

-- Table: public.books 

-- DROP TABLE public.books; 

CREATE TABLE public.books 
(
    id integer, 
    data json 
) 
WITH (
    OIDS=FALSE 
); 
ALTER TABLE public.books 
    OWNER TO postgres; 

-- Index: public.books_author_first_name 

-- DROP INDEX public.books_author_first_name; 

CREATE UNIQUE INDEX books_author_first_name 
    ON public.books 
    USING btree 
    (((data -> 'author'::text) ->> 'first_name'::text) COLLATE pg_catalog."default"); 

select * from books; 

enter image description here

從laravel:

$testPJSON=DB::select('SELECT * FROM books WHERE data->>"last_name" = "White"'); 

越來越空; 從Postgres JSON類型獲取Laravel中數據的最佳方式是什麼?

回答

1

您的查詢應該與數據匹配。 JSON對象中沒有元素last_name

試試這個SQL語句:

$testP2=DB::select(
      "SELECT * FROM books WHERE data->'author'->>'last_name' = 'White'" 
     );