1

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">&times;</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

27
  • What does $rooms = InsertRoom::all(); dd($rooms); show? Commented May 10, 2016 at 5:02
  • @AlexeyMezenin Basically I want to retrieve all the rows from the insert_rooms table and store it in the object $rooms. Then the variable $rooms is passed into the view and by using '$room->roomid' I try to obtain only the room id's which I want to display. Hope I was clear Commented May 10, 2016 at 5:12
  • I see this. ) To be able to help you, I need to debug it with you. Please use code from my previous comment and tell me the output. Commented May 10, 2016 at 5:13
  • @AlexeyMezenin Sorry, my bad.. I thought you were asking what the code does. When I entered dd($rooms), I got the following output: Collection {#207 ▼ #items: array:13 [▶] } Commented May 10, 2016 at 5:18
  • Ok, now remove dd and add this into the view (before foreach): {{ dd(isset($rooms)) }} Commented May 10, 2016 at 5:20

3 Answers 3

2

You should try

<div class="border1">
@if(isset($rooms))
    @foreach($rooms as $room)
        <a href="#">{{$room->roomid}}</a>
    @endforeach
@endif

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

1 Comment

Well... It works now. I had to surround the $room->roomid with the double curly brackets... I feel so stupid for forgetting that. Thanks a lot for the help
0

Typo Error: rooms instead of results. You haven't passed any variable named 'results' from your controller to view.

You just need to check if result object(rooms) is set or not. You can use any of below to do this.

All will return boolean result true or false.

@if($rooms)
    @foreach($rooms as $room)
        <a href="#">{{$room->roomid}}</a>
    @endforeach
@endif

OR

@if(!empty($rooms))
    @foreach($rooms as $room)
        <a href="#">{{$room->roomid}}</a>
    @endforeach
@endif

OR

@if(isset($rooms))
    @foreach($rooms as $room)
        <a href="#">{{$room->roomid}}</a>
    @endforeach
@endif

Comments

0

Instead of doing this: return view('dashboard', ['rooms' => $rooms]);

Just print out all your rooms in your get view function. And then in your insertRooms function do a: return redirect()->back();

Instead then your get view will automatically print out the new values because of a page refresh :-)

And please start using the compact method to return variables to your views, i has been best practice sine laravel 5.1 :-)

Also i would advice you to watch some videos on nameing conventions. Best of luck!

2 Comments

Thank you for your help! Is there a reason that compact method is used to return variables? Also, should I downgrade to Laravel 5.1? When reading about Laravel, I heard something about Laravel 5.2 will soon become obsolete, but I dont understand that because shouldn't Laravel 5.1 become obsolete before 5.2?
To my knowledge and my own experience it's mainly due to readability through out the code. I have just upgraded a project to 5.2 so i hope not :D i havent heard anything about it :-) if this solved your answer please accept it thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.