Created
June 7, 2016 16:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Model; | |
class Category extends Model{ | |
public function subcategories(){ | |
return this->hasMany('App\Subcategory') | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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