Reputation: 820
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? :)
Upvotes: 1
Views: 23628
Reputation: 1615
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']);
}
Upvotes: 4
Reputation: 5649
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
Upvotes: 1
Reputation: 1169
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.
Upvotes: 5