【发布时间】: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')->with('union', $results);和foreach($unions as $union)