Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nero's avatar
Level 1

Form Edit Blade Sub Category Dynamic in Laravel

Ask for assistance. I have a small app for news apps. I am using ajax json dynamic category. The process of data storage has been successful. So for example when we select the sub categories Entertainment category automatically appear based on those categories eg appear sub category Music and Movie.

Now my problem is not yet solved is the section Change Data Form (Edit). When I select a category on the form sub-category system does not display data sub categories based kategoti selected.

Please help from the masters. Understandably still a beginner.

Cases here I want to change into Sub Categories Movie Music.

The following summary of the script program in laravel. Please correction of the master.

The database structure: Tabel categoriess: id,category_name. Tabel subcategoriess:id,category_id,subcategory_name. Tabel Newss:id,subcategory_id,subject,description

View:

{!! Form::model($news,array('url'=>'event','files'=>'true'))!!}

{!! Form::select('category', $categories, null, ['id' => 'category', 'class' => 'form-control category')]) !!}

<option>

JS:

var $ = jQuery.noConflict(); $('#category').change(function() { $.get('categories/' + this.value + '/subcategories.json', function(subcategories) { var $subcategory = $('#subcategory');

$subcategory.find('option').remove().end();

$.each(subcategories, function(index, subcategory) { $subcategory.append('' + subcategory.subcategory_name + ''); }); }); });

$(document).ready(function() { $(".category option[value='0']").attr("disabled","disabled"); $(".subcategory option[value='0']").attr("disabled","disabled"); });

Routes:

Route::get('news/{id}/categories/{code}/subcategories.json', function($code) { return \App\Subcategory::where('category_id', $code)->get(); });

Controllers:

$data['news']=News::find($id); $categories =Category::lists('category_name', 'id')->all(); $subcategories =Subcategory::lists('subcategory_name', 'id')->all(); return view('news.edit',$data)->with(compact('categories'))->with(compact('subcategories'));

File view edit.blade.php I save in the directory resource / view / news / edit.blade.php.

0 likes
0 replies

Please or to participate in this conversation.