0

I created table

-- 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");

and

select * from books;

enter image description here

from the laravel:

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

getting null; what is the best way to get data in Laravel from Postgres JSON type?

1 Answer 1

1

Your query should match the data. There is no element last_name in the JSON object.

Try this SQL statement:

$testP2=DB::select(
          "SELECT * FROM books WHERE data->'author'->>'last_name' = 'White'"
         );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.