在 laravel 中查看错误未定义变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27747977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:37:52  来源:igfitidea点击:

View in laravel error undefined variable

phplaravel

提问by kaern12

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

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

routes.php

路由文件

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

});

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

BookController.php

图书控制器.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

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

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

回答by lukasgeiter

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.

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

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 $resultsexists in your view, since you also want to display it when no results are available

您还需要检查$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

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 Eloquentor the query builder. At the very minimum, use bindings for parameters:

安德烈·丹尼尔 (André Daniel) 的评论非常正确。您的代码容易受到 SQL 注入。你真的应该看看Laravels ORM口才查询生成器。至少,使用参数绑定:

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)

这是查询构建器的示例(感谢@AndréDaniel)

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

回答by Corentin

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

routes.php

你必须用你的变量来表达你的观点。
这是你应该做的:

routes.php

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

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

BookController.php

图书控制器.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

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

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