Answer the question
In order to leave comments, you need to log in
Who can help me deal with routes in laravel?
Guys. Tell me please. Here is my trace. situation. With /portfolio, my portfolio view is returned. Now, I need to do a check somehow, or something, so that if at the url 'portfolio/logos, portfolio/web' I return the portfolio view, but at the same time, a selection of works from the database was made by the category name, i.e. in the example "web","logos" and display these works...
And if the url /portfolio/work-name, where "work-name" is the name of the work, another view was displayed, for example "work"?
Now things are like this:
I have three routes (below). /portfolio returns a portfolio view. But. If /portfolio/work-name then it tries to return to me a page with works corresponding to the category, although it needs to return a page with the output of one work.
And another question. In my "categories" column in the database, from one to several categories are stored, like "cat1, cat2, cat3", etc. How to be in that case? How to return the work of the corresponding category?
Three routes:
Route::get('/portfolio', '[email protected]'); //должна вернуться страница со всеми работами
Route::get('/portfolio/{category?}', '[email protected]'); //должна вернуться страница с работами, соответствующей категории.
Route::get('/portfolio/{work?}', '[email protected]'); //должна вернуться работа
public function portfolio($category = null)
{
if(is_null($category )) {
$portfolio = Works::paginate(10);
}
else {
$portfolio = Works::where("categories", $category )->get();
return view("portfolio", compact('portfolio'));
}
return view("portfolio", compact('portfolio'));
}
Answer the question
In order to leave comments, you need to log in
Put separately? 'portfolio/logos and portfolio/web' above /portfolio/{work-name}.
and write in the controller a separate function for the first two, for your own work-name.
Use the "magic" sign ?
Accordingly, in the controller
public function index($catergory = null){
if(is_null($catergory )) {
$portfolio = Portfolio:paginate(10);
} else {
$portfolio = Portfolio::where("catergory", $catergory )->paginate(10);
}
return view("portfolio", compact('portfolio'));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question