【问题标题】:Laravel Query Exception ErrorLaravel 查询异常错误
【发布时间】:2015-02-28 16:29:09
【问题描述】:

我正在尝试运行以下“Results()”查询并在我看来与 foreach 循环相关的以下错误:

public function index()
    {

        //show view of homepage
        return View::make('index');
    }

    public function results()
    {   
        $query = DB::table('Unions'); // Get the table before applying the where clauses

        if (Request::get('Affilation')) {
            $Affilation = Input::get('Affilation');
            if ($Affilation != null) {
                $query = $query->where('Affilation', $Affilation);
            }
        }

        if (Request::get('Office_State')) {
            $Office_State = Input::get('Office_State');
            if ($Office_State != null) {
                $query = $query->where('Office_State', $Office_State);
            }
        }

        if (Request::has('Local_Name')) {
            $Local_Name = Input::get('Local_Name');
            if ($Local_Name != null) {
                $query = $query->where('Local_Name', $Local_Name);
            }
        }

        $results = $query->get(); // Calling get() will execute the query, so it must be last to be called
        //show results from union search
        return View::make('results')->with('union', $results);
     } 



    public function profile($Local_Name)
    {

            $union = Union::whereLocal_name($Local_Name)->first();

        //show individual union profile
        return View::make('profile', ['union' => $union]);
        } 

这是一个很大的错误:

[2015-02-28 15:21:48] 生产。错误:异常'ErrorException' 带有消息“未定义的变量:联合” C:\xampp\htdocs\laravel\app\storage\views\828b81503d4e5a41a8d04770b175c57d:44

堆栈跟踪:

#0 C:\xampp\htdocs\laravel\app\storage\views\828b81503d4e5a41a8d04770b175c57d(44): Illuminate\Exception\Handler->handleError(8, 'Undefined varia...', 'C:\\xampp\\htdocs...', 44, Array)
#1 C:\xampp\htdocs\laravel\bootstrap\compiled.php(9876): include('C:\\xampp\\htdocs...')
#2 C:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\View\Engines\CompilerEngine.php(56): Illuminate\View\Engines\PhpEngine->evaluatePath('C:\\xampp\\htdocs...', Array)
#3 C:\xampp\htdocs\laravel\bootstrap\compiled.php(9754): Illuminate\View\Engines\CompilerEngine->get('C:\\xampp\\htdocs...', Array)
#4 C:\xampp\htdocs\laravel\bootstrap\compiled.php(9741): Illuminate\View\View->getContents()
#5 C:\xampp\htdocs\laravel\bootstrap\compiled.php(9732): Illuminate\View\View->renderContents()
#6 C:\xampp\htdocs\laravel\bootstrap\compiled.php(10434): Illuminate\View\View->render()
#7 C:\xampp\htdocs\laravel\bootstrap\compiled.php(9964): Illuminate\Http\Response->setContent(Object(Illuminate\View\View))

这是被引用的第 44 行视图:

<?php if($unions->count()): ?>
        <?php foreach($unions as $union): ?>

【问题讨论】:

  • 您将union 传递给视图并尝试使用unions 访问它。
  • @lukasgeiter 你是说我的 foreach 循环不正确吗?这个查询之前很简单,就像这样: public function results() { $query = Request::get('Affilation'); $工会= $查询? Union::where('Affilation', 'LIKE', "$query")->get() : Union::all(); //显示联合搜索的结果 return View::make('results')->withUnions($unions); }
  • 不,我是说问题出在变量名上。 return View::make('results')-&gt;with('union', $results);foreach($unions as $union)

标签: php laravel eloquent


【解决方案1】:

您正在返回一个视图:

union

而你正在尝试 foreach

unions

试试return View::make('results')-&gt;with('unions', $results);

【讨论】:

  • 更改后我的 laravel.log 仍然指向第 44 行,只是它说的是不同的内容:[2015-02-28 16:42:32] production.ERROR: exception 'Symfony\Component \Debug\Exception\FatalErrorException' 在 C:\xampp\htdocs\laravel\app\storage\views\828b81503d4e5a41a8d04770b175c57d:44 中带有消息“调用非对象上的成员函数 count()” 堆栈跟踪:#0 [内部函数]: Illuminate\Exception\Handler->handleShutdown() #1 {main} [] []
  • 感谢你们。这修复了错误。至于之后产生的错误,我去掉了->Count()。我仍然没有从我的数据库中得到响应。希望这对其他人有帮助。
猜你喜欢
  • 1970-01-01
  • 2019-09-06
  • 2018-04-07
  • 2021-10-25
  • 1970-01-01
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多