Skip to content

Instantly share code, notes, and snippets.

@josephrexme
Created June 7, 2016 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephrexme/6b6d3d4e1a753c62f5c2741941791ad9 to your computer and use it in GitHub Desktop.
Save josephrexme/6b6d3d4e1a753c62f5c2741941791ad9 to your computer and use it in GitHub Desktop.
Dependent dropdown in Laravel5 PHP. For full article check here http://josephrex.me/implementing-dynamic-drop-down-or-dependent-list-in-laravel4
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Category extends Model{
public function subcategories(){
return this->hasMany('App\Subcategory')
}
}
<?php
use App\Category;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CategoryController extends Controller{
public function api(Request $request){
$input = $request->input('option');
$category = Category::find($input);
return response()->json($category->subcategories);
}
}
<?php
Route::get('api/dropdown', [
'uses' => 'CategoryController@api',
'as' => 'category_api'
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment