1

I have a problem with this.

Undefined variable: acara (View: /var/www/html/event_organizer/resources/views/admin/home.blade.php)

But I have declared $acara on the controller like this

Controller

use App\events; 

    public function lihat_acara()
    {
      $data['acara'] = events::all();
      return view('admin.home')->with($data);
    }

and the view like this

home.blade.php

@foreach($acara as $key)
  <tbody>
    <tr>
      <td>{{ $key->nama_acara }}</td>
      <td>{{ $key->kategori_acara }}</td>
      <td>{{ $key->lokasi_acara }}</td>
    </tr>
  </tbody>
  @endforeach

What's wrong with my code? Any idea? :)

1
  • 1
    Controller: view('admin.home', compact("data"));. In your home.blade.php, @foreach($data["acara"] as $key) Commented Mar 13, 2018 at 5:29

3 Answers 3

5
use App\events; 

public function lihat_acara()
{
  $data['acara'] = events::all();
  return view('admin.home', ['acara' => $data['acara'] ]);
}

OR

use App\events; 

public function lihat_acara()
{
  $data['acara'] = events::all();
  return view('admin.home')->with('acara', $data['acara']);
}

OR

use App\events; 

public function lihat_acara()
{
  $data['acara'] = events::all();
  return view('admin.home')->withAcara($data['acara']);
}

From this laravel official site Passing data to view, you can find all the way to send data from controller to view.

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

4 Comments

yeah, of course, please see 1st case. that will give the array. you changed your answer format like me. :)
hmm... I think you need to read the official document again. first case data is second parameter of view function. second case, view('viewname')->with('variablename', value). that's not the same type.
Yeah, thanks :). I've try your suggestion and it work too
4

You need to change with() it's take an array

Change

with(['data'=>$data]);

or you can do like this also

withData($data);

Now you can receive the variable in view with $data and in your case it will be

use App\events; 

    public function lihat_acara()
    {
      $data['acara'] = events::all();
      return view('admin.home')->with(['acara'=>$data['acara']]);
    }

Or

use App\events; 

    public function lihat_acara()
    {
      $data['acara'] = events::all();
      return view('admin.home')->withAcara($data['acara']);
    }

2 Comments

we need to pass variable and value(two parameters) into with() function. or you can pass the array of variables as second parameter of view() function
you've updated your answer several time along my answer. and why did you remove all comment you wrote to my answer?
1

In the scenario above, the acara is not a variable you declared acara as an array key. I don't know why you had to do it like this. You can simply write the code like this:

$acara = Event::all();

OR

If you want to keep the controller like that, change the variable in @foreach loop:

@foreach($data['acara'] as $key)
<tbody>
    <tr>
        <td>{{ $key->nama_acara }}</td>
        <td>{{ $key->kategori_acara }}</td>
        <td>{{ $key->lokasi_acara }}</td>
    </tr>
</tbody>
@endforeach

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.