Laravel依赖下拉

时间:2018-08-30 16:12:32

标签: javascript laravel

我很想适应这种情况,我很困惑。

  • 我需要将父级更改为account_id
  • 我的代码中已经包含$ categories,我需要选择另一个变量
  • 我需要将parent为null更改为account_id = 0

方法:我需要将父级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' ]);

谢谢..

0 个答案:

没有答案