php - Laravel custom controller method calling -


i need basic help, new laravel.

situation

i have controller articlescontroller has index method lists articles , it's working fine. route below, allows me show individual articles 'id' articles/id example articles/35 . need show articles category "swimming" . when hit articles/swimming id=swimming. don't know how make custom route list articles "swimming" category. have made method "swimming" inside controller need route?

  route::bind('articles', function($value, $route) {      return $list = app\article::whereid($value)->first();      }); 

you can create/use 2 separate routes, 1 articles id , articles category, example, routes declared like:

route::get('articles', 'articlecontroller@index');  route::get('articles/{id}', 'articlecontroller@getbyid')->where('id', '[0-9]+');  route::get('articles/{category}', 'articlecontroller@getbycategory')->where('category', '[a-za-z]+'); 

then may use controller methods (in articlecontroller.php):

public function index() {     return app\article::all(); }  public function getbyid($id) {     return app\article::whereid($id)->first(); }  public function getbycategory($category) {     return app\article::with(['category' => function($q) use($category)     {        $q->where('category', $category);      }])->get();  } 

this basic idea, can improve it, anyways.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -