Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dickyperdian's avatar

GEt array

https://pastebin.com/KkGKmBQd =array

how i get judul in view, i try @foreach ($blocks as $object) {{ $blocks->judul }} @endforeach and @foreach ($blocks as $object){{ $object['figure_id'] }} @endforeach but i got error Undefined index: figure_id (View: /home/kevinhermawan/Music/FULL BACKUP FROM 159/aspirasi-web/resources/views/pages/pemilihan/mulai-page.blade.php)

my script controller https://pastebin.com/pPk6gzaH

i need helpp :( please, thanks before

0 likes
27 replies
bashy's avatar

Please format your code in 3x ` or ~ at the start and end.

You can also post the array and controller code here the same way.

2 likes
bipin's avatar

@Dickyperdian this error is getting because your front view has figure_id variable and your controller not sending it for eg at controller

        public function index(){
               $data='hi hi hi';
             return view('welcome',compact('data'));
      }

at welcome view

    <h1>{{$total}}</h1>// as you can see this value is not return by controller
        @foreach($data as $d)
              {{$d}}
       @endforeach 
1 like
Dickyperdian's avatar

i want get data return view('pages/pemilihan/mulai-page',['blocks' => $blocks], $data);

blocks, how :(

BezhanSalleh's avatar

@Dickyperdian in your controller change

 return view('pages/pemilihan/mulai-page',['blocks' => $blocks], $data);

to

return view('pages/pemilihan/mulai-page',compact('blocks','data'));

now you can access the $data and $blocks in your view.

1 like
BezhanSalleh's avatar

post your view and controller by typing ``` then your codes here ``` then click on post your reply

1 like
BezhanSalleh's avatar

post it here as i told you: so if anyone opens your question they could see it here not by going on another site and provide you with a solution that way it will be faster. anyways try to dd($blocks) and check if you have a property there called judul...

1 like
Dickyperdian's avatar

view

   <div class="card" style="width: 18rem;">
   <img class="card-img-top" src="" alt="Card image cap">
   <div class="card-body">
    <h5 class="card-title">{{ $object->judul }}</h5>
     <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
  <a href="#" class="btn btn-primary">Go somewhere</a>
</div>
 </div>
@endforeach  ```


controller

``` $blocks = array();
        foreach($hostels as $hostel) {
            $blocks[] = DB::table('pasangan_calon')->where('id', $hostel->pasangan_calon_id)->get();
        }
 
       return view('pages/pemilihan/mulai-page', compact('blocks', 'data', 'election'));   ```
BezhanSalleh's avatar

you need to access it like $object['judul'] or dd($blocks) and post the result here. to see if you have that property in your blocks array or not...

1 like
Dickyperdian's avatar

Undefined index: judul , and dd($blocks)

array:1 [▼
  0 => Collection {#249 ▼
    #items: array:1 [▼
      0 => {#245 ▼
        +"id": 7
        +"figure_id": 1
        +"figure_id2": null
        +"description": "PPP"
        +"thumbnail_id": "pasangan/d5fb88fa-047b-11e8-97b6-e82aea7dcca6"
        +"thumbnail_url": "http://res.cloudinary.com/aspirasi/image/upload/v1517179056/pasangan/d5fb88fa-047b-11e8-97b6-e82aea7dcca6.svg"
        +"uuid": "d5fb88fa-047b-11e8-97b6-e82aea7dcca6"
        +"created_by": 3
        +"updated_by": 3
        +"created_at": "2018-01-29"
        +"updated_at": "2018-01-29"
        +"slug": null
        +"moto": "PPP"
        +"status": "1"
        +"judul": "Paslon PPP"
      }
    ]
  }
]
BezhanSalleh's avatar

you have a collection inside an array... in your controller change ->get() to toArray(); or use pluck if you don't need all the columns pluck('judul')

1 like
BezhanSalleh's avatar
Level 25

ok @Dickyperdian in your controller, remove $block = array(); and change the code inside the foreach as follows:

   foreach($hostels as $hostel) {
            $blocks = collect(DB::table('pasangan_calon')->where('id', $hostel->pasangan_calon_id)->get());
        }

here instead of storing your data inside an array we are storing it inside a collection, in your view as is, you are iterating over a collection. so just update your controller you will be good to go. for example;

@if(count($blocks)>0)
      @foreach($blocks as $block)
           {{$block->judul}}
           //now you can access anything inside the blocks as $block->propertyName
      @endforeach
@else
    No data 
@endif

~cheers

1 like
BezhanSalleh's avatar

that is it. your problem is solved. mark the answer to close the thread

1 like
BezhanSalleh's avatar

@bashy haha :) saw your replies ... you are right though! but had some time to kill so ;)

~cheers mate!

1 like
BezhanSalleh's avatar

Since your first problem is solved.

create a new question, do not post it here if you want to get any quick response.

explain what are you trying to achieve and what is the problem?...

you see how this works is, you explain your problem and show your code and we try to help you with your problem.

1 like

Please or to participate in this conversation.