Question

I have taken a few answers from both SO and the Laravel forums to piece together the results. However, I am having an Undefined variable error and I'm not sure why.

Controller

public function getJobs()

        {
            $query = DB::table('jobs');

            if (isset($jcategory))
            $query->where('category', '=', $jcategory);

            $result = $query->get();
           return View::make('jobsearch.search', $result);
        }

View (has been abbreviated for space)

<form action="{{ action('JobsearchController@getJobs') }}" method="post">
  <div class="row">
    <div class="large-8 columns">
      <input type="text" name="realm" placeholder="Keywords/Skills" />
    </div>
    <div class="large-4 columns">
        <select name="jcategory">
        <option value="">Select Category</option>
        <option value="jc1">Accounting</option>

...

 </select>
    </div>
    <div class="large-4 columns">
       <input type="submit" value="Search" style="width:100%; padding-top: .5rem;
padding-bottom: .5rem;" class="button border-btn" />
      </div>


</div>
</form>

@foreach ($result as $job)
    <p>{{ $job->id }}</p>
@endforeach

Once I get this little test working I will add the rest of the isset into the $query. What I am trying to achieve is that the view will load all jobs in the database and if the user changes the category (for example) and submits the view it will reload the page with the reflected changes in the query.

I currently get an Undefined variable: result error on the view. I have tried the foreach in different ways but I have a feel that the controller isn't passing anything through.

Was it helpful?

Solution

You passed the $result variable the wrong way. Try this:

return View::make('jobsearch.search')->with('result', $result);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top