使用laravel选择类别后选择子类别 [英] Selecting a sub-category after selecting category using laravel
问题描述
我正在创建一种表单,在选择一个类别后,我将在其中获得子类别.但是问题是我只有一个表,用于存储类别和子类别.
I am creating a form in which I get subcategories after I select one category. But problem is that I have only one table in which categories and subcategories are stored.
表中的所有值都有其自动增量id
和parent_id
.对于所有Main-Categories
,parent_id
是0
.和Sub-categories
具有Main-Categories
id
作为其parent_id
.如图所示
All the values in the table have their auto-increment id
and a parent_id
. For all Main-Categories
, parent_id
is 0
. and Sub-categories
have Main-Categories
id
as their parent_id
. As shown in image
根据这种情况,我构建了一个查询,并将该查询通过控制器传递给我的视图
Depending upon this condition I build a query and passed that query to my view through controller as
$k = DB::table('main_category')->where('parent_id','=','0')->lists('name','id');
而且在视野范围内:
<div class="form-group">
{!! Form::label('category','Category:') !!}
<select name="category" id="category" class="form-control input-sm">
@foreach($k as $a)
<option value="{{$a}}">{{$a}}</option>
@endforeach
</select>
</div>
<div class="form-group">
{!! Form::label('subcategory','Subcategory:') !!}
<select name="subcategory" id="subcategory" class="form-control input-sm">
<option value=""></option>
</select>
</div>
我的 script.js:
$('#category').on(change,function(e){
console.log(e);
var cat_id = e.target.value;
//ajax
$get('/ajax-subcat?cat_id='+ cat_id,function(data){
//success data
//console.log(data);
$('#subcategory').empty();
$.each(data,function(index,subcatObj){
$('#subcategory').append('<option value ="'+subcatObj.id+'">'+subcatObj.name+'</option>');
});
});
});
Routes.php:
Route::resource('event', 'EventsController');
Route::get('/ajax-subcat',function () {
$cat_id = Input::get('cat_id');
// return $cat_id;
$subcategories = DB::table('main_category')->where('name', '=',$cat_id)->get();
return Response::json($subcategories);
});
但是当我使用$a->name
或$a->id
时,尝试获取非对象属性会出现错误.
But when I use $a->name
or $a->id
, I get error as trying to get non object property.
我也很欣赏这部视频
推荐答案
Here are some corrections taking reference from this page for script. In Controller:
$s = Category::all()->where('parent_id','=','0');
查看:
<div class="form-group">
{!! Form::label('category','Category:') !!}
<select name="category" id="category" class="form-control input-sm">
@foreach($s as $k)
<option value="{{ $k['id'] }}">{{ $k['name'] }}</option>
@endforeach
{{--<option value="Dance And Music">Dance And Music</option>--}}
</select>
</div>
<div class="form-group">
{!! Form::label('subcategory','Subcategory:') !!}
<select name="subcategory" id="subcategory" class="form-control input-sm">
<option value=""></option>
</select>
</div>
脚本:
$(document).ready(function () {
$('#category').on('change',function(e){
console.log(e);
var cat_id = e.target.value;
//console.log(cat_id);
//ajax
$.get('/ajax-subcat?cat_id='+ cat_id,function(data){
//success data
//console.log(data);
var subcat = $('#subcategory').empty();
$.each(data,function(create,subcatObj){
var option = $('<option/>', {id:create, value:subcatObj});
subcat.append('<option value ="'+subcatObj+'">'+subcatObj+'</option>');
});
});
});
});
Routes.php
Routes.php
Route::get('/ajax-subcat',function () {
$cat_id = Input::get('cat_id');
$subcategories = DB::table('main_category')->where('parent_id','=',$cat_id)->lists('name');
return Response::json($subcategories);});
这篇关于使用laravel选择类别后选择子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!