Monday, March 6, 2017

laravel undefine variable : category

Please help me; I am new to Laravel, and I'm not getting what I'm doing wrong.

Here is my view:

<div class="container">
  <h1>category and subcategory</h1>
  <div class="row">
    <div class="col-md-10 col-md-offset-1">
      {!! Form::open(array('url' => '','files'=>true)) !!} {!! Form::token(); !!}
      <div class="form-group">
        <label for="">category</label>
        <select class="form-control input-sm" name="" id="">
          @foreach ( $category as $category)
          <option value=""></option>
          @endforeach
        </select>
      </div>
      <div class="form-group">
        <label for="">subcategory</label>
        <select class="form-control input-sm" name="" id="">
          <option value=""></option>
        </select>
      </div>
      </form>
    </div>
  </div>
  <script type="text/javascript">
    $(document).ready(function() {
          $("#category").on('change', function(e) {
            console.log(e);
            var cat_id = e.target.value;
            $.get('/ajax-subcat/' + cat_id, function(data) {
              //success data
              console.log(data);
            })
          });
  </script>
</div>
</div>
</div> 

Here is my route:

Route::get('/', function() {
  $category = \App\ category::all();
  return view('layouts.app') - > with('categories', $category);
});
Route::get('/ajax-subcat', function() {
  $category = \App\ category::all();
  $cat_id = Input::get('cat_id');
  $subcategory = subcategory::where('category_id', '=', $cat_id) - > orderBy('name', 'asc') - > get();
  return response() - > json($subcategory);
});

And this is my error log file:

ErrorException in ec195dc7ec967851481b4815c28c6879244d5d45.phpline 87: Undefined variable: category(View: C:\xampp\htdocs\laravel\resources\views\layouts\app.blade.php) (View: C:\xampp\htdocs\laravel\resources\views\layouts\app.blade.php) in ec195dc7ec967851481b4815c28c6879244d5d45.php line 87 at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44 at PhpEngine-

What is causing these errors?



via Bipin Shukla

Advertisement