这是我的vew代码我在laravel中的新东西我没有得到我做错了什么 请帮助我尝试每件事我得到的undefine变量:类别错误
<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="{{$category->id}}">{{$category->name}}</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>
这是我的路线
Route::get('/', function () {
$category=\App\category::all();
return view('layouts.app')->with ('category',$category);
});
Route::get('/ajax-subcat',function(){
$cat_id = Input::get('cat_id');
$subcategory = subcategory::where('category_id', '=', $cat_id)
->orderBy('name', 'asc')
->get();
return response()->json($subcategory);
});
这是错误日志:
ec195dc7ec967851481b4815c28c6879244d5d45.php第87行中的ErrorException: 未定义的变量:category(查看:C:\ xampp \ htdocs \ laravel \ resources \ views \ layouts \ app.blade.php)(查看:C:\ xampp \ htdocs \ laravel \ resources \ views \ layouts \ app.blade。 PHP)
有没有办法检查数据流?像dot net中的明智仍然得到相同的错误请帮助以及会导致此类错误的原因。
答案 0 :(得分:0)
您忘记添加输入的ID:
<select class="form-control input-sm" name="" id="">
将其修改为:
<select class="form-control input-sm" name="" id="category">