I am a bit of a newbie to both PHP and Laravel and I trying to build simple Web Apps in order to familiarize myself with both PHP and Laravel. I am getting an undefined variable in blade file. It would be appreciated if someone could help me clear this up.
Basically, what I want to happen is:
I have a View called dashboard.blade.php and in this view there is a text field and button. When the room number is entered and the button is clicked, the room number must get saved in the database and the current view needs to get refreshed but this time, the new room has to be shown at the top. The room gets saved in the database without a problem, but when I try to retrieve it using the foreach loop, I get the 'undefined variable rooms' error
I am using Laravel 5.2
The button concerned with firing off this action (in dashboard.blade.php)
<form action="{{ route('viewroom') }}" method="POST"> //route is viewroom
Please Insert The New Room Number<br>
<input type="text" name="roomid"/>
<button type="submit">Add New Room</button>
<input type="hidden" value="{{ Session::token() }}" name="_token"/>
</form>
Route file
Route::post('/dashboard',[
'uses' => 'RoomController@InsertRoom',
'as' => 'viewroom'
]);
Function used in the RoomController
public function InsertRoom(Request $request){
$this->validate($request, [
'roomid' => 'required | numeric | unique:insert_rooms',
]);
$room = new InsertRoom();
$room->roomid = $request['roomid'];
$room->status = 0;
$room->save();
$rooms = InsertRoom::all();
$request->session()->flash('alert-success', 'Room was successful added!');
return view('dashboard', ['rooms' => $rooms]);
}
In dashboard.blade.php:
<div class="border1">
@if(isset($results))
@foreach($rooms as $room)
<a href="#">$room->roomid</a>
@endforeach
@endif
</div>
Any and all help would be appreciated. Thank you
UPDATE I forgot to clarify that, I got the error 'undefined variable $rooms' BEFORE I used an if condition to check if $errors was set. After I used if(isset($errors)) I didnt get the error anymore but the information I wanted to show up didn't show up
UPDATE 2
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dashboard</title>
<style>
h1{
text-align: center;
}
.border1{
border: dotted;
border-color: red;
text-align: center;
padding: 20px;
}
.ulerror{
text-align: center;
color: red;
}
.flash-message{
text-align: center;
font-weight: bolder;
color: lawngreen;
}
ul li{
font-family: "Lucida Console";
font-size: 24px;
list-style: none;
padding: 10px;
}
a{
padding: 8px;
}
</style>
</head>
<body>
<h1>Dashboard</h1>
<div class="border1">
@if(isset($rooms))
{{--{{ dd(isset($rooms)) }}--}}
@foreach($rooms as $room)
<a href="#">{{ $room->roomid }}</a>
@endforeach
@endif
{{--<a href="{{ route('roomdetails', ['id' => '1', 'status' => 'free']) }}">1</a> IGNORE THIS--}}
</div>
<br><br><br>
<div class="border1">
<b>Insert a new Room</b>
<br><br>
<form action="{{ route('viewroom') }}" method="POST">
Please Insert The New Room Number<br>
<input type="text" name="roomid"/>
<button type="submit">Add New Room</button>
<input type="hidden" value="{{ Session::token() }}" name="_token"/>
</form>
</div>
<div class="flash-message">
@foreach (['danger', 'warning', 'success', 'info'] as $msg)
@if(Session::has('alert-' . $msg))
<p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} <a href="{{ route('backhome') }}" class="close" data-dismiss="alert" aria-label="close">×</a></p>
@endif
@endforeach
</div> {{--flash message--}}
@if(count($errors) > 0)
<div class="ulerror">
<ul>
@foreach($errors->all() as $error)
{{ $error }}
@endforeach
</ul>
</div> {{--error handling--}}
@endif
$rooms = InsertRoom::all(); dd($rooms);show?{{ dd(isset($rooms)) }}