简体   繁体   中英

Laravel Query Exception Error

I'm trying to run the following "Results()" query and getting the following error related to a foreach loop in my view:

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

This is a large chuck of the error:

[2015-02-28 15:21:48] production.ERROR: exception 'ErrorException' with message 'Undefined variable: unions' in C:\\xampp\\htdocs\\laravel\\app\\storage\\views\\828b81503d4e5a41a8d04770b175c57d:44

Stack trace:

#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))

And this is the view line 44 being referenced:

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

You are returning a view with:

union

And you are trying to foreach

unions

Try return View::make('results')->with('unions', $results);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM