查看 laravel 错误未定义的变量

View in laravel error undefined variable

我是 laravel 和 php 的新手。我无法在 laravel 中显示查询结果。我在我的程序中写了这个。

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>

错误是未定义的变量:结果(视图:...\absen.blade.php)我对此感到非常厌倦。请帮忙

您应该在 absen() 操作中呈现视图,而不是重定向。进行重定向时,您刚刚从数据库中选择的数据都消失了。

试试这个:

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);
}

您还需要检查 $results 是否存在于您的视图中,因为您还想在没有可用结果时显示它

@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

关注

André Daniel 的评论非常正确。您的代码容易 SQL 注入。你真的应该看看 Laravels ORM Eloquent or the query builder。至少,对参数使用绑定:

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

这是一个查询生成器的示例(感谢@AndréDaniel)

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

你必须用你的变量来制作你的视图。
这是你应该做的:

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

顺便说一下,你应该使用 Laravel 表单函数 Forms & HTML