I'm beginner with laravel.
I have a model "Movie" with several categories like western, suspense, comedy, drama... When I create new comedy, I use the route: Route::get('/movie/comedy', 'MovieController@createMovie');
In the MovieController, the method: public function createMovie() { $actors = Actor::all(); $category_id = 1; $title = "New comedy"; return view('movie.create', compact('category_id', 'actors', 'title')); }
I'd like to use the same method createMovie, but with differents values of category_id and title using parameters when I create the route. For example :
Route::get('/movie/drama', 'MovieController@createMovie') and parameters $category_id = 2, $title = "New drama";
Route::get('/movie/western', 'MovieController@createMovie') $category_id = 3, $title = "New wester";
public function createMovie($category_id, $title) { $actors = Actor::all(); return view('movie.create', compact('category_id', 'actors', 'title')); }
hermanoloco said:
I'm beginner with laravel.
I have a model "Movie" with several categories like western, suspense, comedy, drama... When I create new comedy, I use the route: Route::get('/movie/comedy', 'MovieController@createMovie');
// route
Route::get('/movie/{category_id}', 'MovieController@create');
// controller
public function create($category_id)
{
$data = [
'categories' => Category::find($category_id)
];
return view('movies.create', $data);
}
the brackets in the route will allow you to pass the value to the controller https://laravel.com/docs/5.3/routing#route-parameters
Thanks w1n78, but I know this solution, my idea is to pass several parameters (minimum 5).
I've found a solution, with Session! I don't know if is the corret but works fine.
Route::get('/movie/western', [ Session::flash('category_id', '2'); Session::flash('title', 'New western'); 'uses' => 'MovieController@createMovie' ]);
public function createMovie() { $actors = Actor::all(); $category_id = Session::get('category_id'); $title = Session::get('title'); return view('movie.create', compact('category_id', 'actors', 'title')); }
Not works fine, because I need to change the value of "Session(title)"
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community