kaern12
kaern12

Reputation: 13

View in laravel error undefined variable

I am a newbie in laravel and php. I cannot show the results of query in laravel. I wrote this in my program.

routes.php

Route::get('books/see', function()
{
    return View::make('books.absen');

});

Route::post('books/absen','BookController@absen');

BookController.php

public function absen()
{
    $ruang = Input::get('ruangn');
    $results = DB::select( DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));

    return Redirect::to('books/see');
}

absen.blade.php

<select name="ruangn" class="form-control" method="post" action="{{URL::to('books/absen')}}">
                    <?php 
                    for( $i=1; $i<19; $i++ )
                    {
                        ?>
                        <option>
                        <?php echo $i;
                    }
                    ?>
                    </option>
                </select>
<input type="submit" name="submit" value="Oke" class="btn btn-info">

<table class="table table-bordered table-striped">
    <thead>
        <th>No</th>
        <th>Name</th>
        <th>ISBN</th>
        <th>TA</th>
        <th>TB</th>
    </thead>
    <tbody>
    @foreach ($results as $value)
        <tr>
            <td>{{$value->id}}</td>
            <td>{{$value->name}}</td>
            <td>{{$value->isbn}}</td>
            <td>{{$value->ta}}</td>
            <td>{{$value->tb}}</td>
        </tr>
    @endforeach
    </tbody>
</table>

And error is Undefined variable: results (View: ...\absen.blade.php) I very tired with this. Help please

Upvotes: 0

Views: 18811

Answers (2)

Corentin
Corentin

Reputation: 149

You have to make your view with your variable.
Here is what you should do :

routes.php

Route::get('books/see', function()
{
    return View::make('books.absen');

});
Route::post('books/absen','BookController@absen');

BookController.php

public function absen()
{
    $ruang = Input::get('ruangn');
    $results = DB::select( DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));

   return View::make('books.absen')->with(array('results' => $results)); 
}

absen.blade.php

<select name="ruangn" class="form-control" method="post" action="{{URL::to('books/absen')}}">
                    <?php 
                    for( $i=1; $i<19; $i++ )
                    {
                        ?>
                        <option>
                        <?php echo $i;
                    }
                    ?>
                    </option>
                </select>
<input type="submit" name="submit" value="Oke" class="btn btn-info">

@if(isset($results))
<table class="table table-bordered table-striped">
    <thead>
        <th>No</th>
        <th>Name</th>
        <th>ISBN</th>
        <th>TA</th>
        <th>TB</th>
    </thead>
    <tbody>
    @foreach ($results as $value)
        <tr>
            <td>{{$value->id}}</td>
            <td>{{$value->name}}</td>
            <td>{{$value->isbn}}</td>
            <td>{{$value->ta}}</td>
            <td>{{$value->tb}}</td>
        </tr>
    @endforeach
    </tbody>
</table>
@endif

By the way, you should use Laravel form functions Forms & HTML

Upvotes: 0

lukasgeiter
lukasgeiter

Reputation: 152860

Instead of redirecting, you should render the view in the absen() action. When doing the redirect the data you just selected from the db is all gone.

Try this:

public function absen()
{
    $ruang = Input::get('ruangn');
    $results = DB::select( DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));

    return View::make('books/see')->with('results', $results);
}

Also you need to check if $results exists in your view, since you also want to display it when no results are available

@if(isset($results))
<table class="table table-bordered table-striped">
    <thead>
        <th>No</th>
        <th>Name</th>
        <th>ISBN</th>
        <th>TA</th>
        <th>TB</th>
    </thead>
    <tbody>
    @foreach ($results as $value)
        <tr>
            <td>{{$value->id}}</td>
            <td>{{$value->name}}</td>
            <td>{{$value->isbn}}</td>
            <td>{{$value->ta}}</td>
            <td>{{$value->tb}}</td>
        </tr>
    @endforeach
    </tbody>
</table>
@endif

Attention

André Daniel is very right with his comment. Your code is prone to SQL injection. You should really take a look at Laravels ORM Eloquent or the query builder. At the very minimum, use bindings for parameters:

DB::select(DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = ?"), array($ruang));

Here's an example with the query builder (Thanks @AndréDaniel)

DB::table("book")->where("ta", $ruang)->get()

Upvotes: 3

Related Questions