我很想适应这种情况,我很困惑。
方法:我需要将父级IS NULL更改为account_id = 0
class CategoriesController extends Controller
{
public function index()
{
$categories = Category::whereRaw('parent IS NULL')->pluck('name', 'id');
return view("categories.index", compact('categories'));
}
public function children(Request $request)
{
return Category::where('parent', $request->parent)->pluck('name', 'id');
}
}
view:我的列是parent_id而不是parent&not null,而是account_id = 0
<div class="form-group">
{!! Form::label('parent', 'Parent Category:')!!}
{!! Form::select('parent', $categories, null, ['placeholder' => 'Choose Category'])!!}
</div>
<div class="form-group">
{!! Form::label('children', 'Child category:')!!}
{!! Form::select('children', [], null, ['placeholder' => 'Choose child category'])!!}
</div>
js
<script>
$('#parent').change(function(e) {
var parent = e.target.value;
$.get('/categories/children?parent=' + parent, function(data) {
$('#children').empty();
$.each(data, function(key, value) {
var option = $("<option></option>")
.attr("value", key)
.text(value);
$('#children').append(option);
});
});
});
路线:
Route::get('/categories', [
'uses' => 'CategoriesController@index',
'as' => 'categories' ]);
Route::get('/categories/children', [
'uses' => 'CategoriesController@children',
'as' => 'categories.children' ]);
谢谢..