0

I want to get array in database,it work in database query (my database i use postgrest) but not work in my project laravel. Error message -> Undefined column: 7 ERROR: column sample1.text[1] does not exist

table sample1
___________________________________
|   id | name |      text[]        |
|------+------+--------------------|
|    1 |   aa | {xxx1,xxx2,xxx3}   |
|    2 |   bb | {xxx1,xxx2,xxx3}   |
|    3 |   cc | {xxx1,xxx2,xxx3}   |
|______|______|____________________|

I need value

{ 
  xxx1,
  xxx1,
  xxx1
}

my code

   $search = DB::table("sample1")->select(array( 'sample1.text[1]'))->get();

    return response()->json($search);

in my database query it work

SELECT "NSO_STAT_GIS"."BND_SET_STAT_TABLE"."MAPTB_CAT_SL_ID"[1] FROM "NSO_STAT_GIS"."BND_SET_STAT_TABLE"
  • 1
    Your text column it is already json. No need to response as json. When you will use it in a foreach will have to use json_decode($model->text); – Militaru Jan 29 '20 at 07:13

1 Answers1

0
$textsJson = DB::table("sample1")->select('text')->get();
$textArray = json_decode($textsJson);

https://laravel.com/docs/5.8/queries#selects

Ricardinho
  • 599
  • 9
  • 22