0

My laravel controller

   public function index()
    { 
    $get=Storage::disk('public')>get('Philippines.json');
    $json = json_decode($get, true); 
           
    return view('BarangayFolder.index')>with('json',$json);
   

the output i'm trying to achieve on my option tag is something like this;

    "BANI"
    "BUYON"
    "CABARUAN"
    "CABULALAAN"
    "CABUSLIGAN"
    "CALIOET-LIBONG"
    "CASILIAN"
    "COROCOR"

i'cant figure out how to get all the of "barangay_list" data, i tried many solution and ideas but still not getting it.

{
      "01": {
        "region_name": "REGION 1",
        "province_list": {
          "ILOCOS NORTE": {
            "municipality_list": {
              "ADAMS": {
                "barangay_list": [
                  "ADAMS (POB.)"
                ]
              },
              "BACARRA": {
                "barangay_list": [
                  "BANI",
                  "BUYON",
                  "CABARUAN",
                  "CABULALAAN",
                  "CABUSLIGAN",
                  "CADARATAN"
                ]
      }

}

i managed to get thedata of "region_name" which is "REGION 1" but i can't get the data of barangay_list.

<datalist id="datalist-content"> 
      foreach (array($json['01']) as $data)
            <option value="{{$data['region_name']}}"></option>
      endforeach
</datalist>
1
  • what is the error? Commented Nov 25, 2020 at 11:27

2 Answers 2

1

You need to understand your json file to get proper data.

You want data of barangay_list. If we create a tree of your data it should look like following:

-01
--province_list
----->ILOCOS NORTE
----------->municipality_list
------------------>BACARRA
--------------------->barangay_list

Either you can use

foreach ($json['01']['province_list']['ILOCOS NORTE']['municipality_list']['BACARRA']['barangay_list'] as $data)
     <option value="{{$data}}"></option>
endforeach

or you can first save you list in variable and use it in loop

@php
       $barangay_list = $json['01']['province_list']['ILOCOS NORTE']['municipality_list']['BACARRA']['barangay_list'];
@endphp
@foreach ($barangay_list as $data)
         <option value="{{$data}}"></option>
@endforeach

You have used true with json_decode that will convert data into array so you dont need to add array function to convert in array.

Sign up to request clarification or add additional context in comments.

Comments

1

your loop is not proper try with below code

<datalist id="datalist-content"> 
             foreach (array($json['01']['province_list']['ILOCOS NORTE']['municipality_list']['BACARRA']['barangay_list']) as $data)
              <option value="{{$data}}">{{$data}}</option>
             endforeach
</datalist>

1 Comment

Thank you for the idea sir sandy, it works well now after removing the array inside foreach.

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.