I cant make this route works
routes.php:
Route::get('/home/category/{$category_id}', 'HomeController@category');
HomeController.php
public function category(Request $request, $category_name, $category_id)
{
$listProject = $this->project->getProjectsFromCategory($category_id);
return view('category', [
'projects' => $listProject,
]);
}
view
<div class="col-md-12">
@foreach ($categories as $key => $cat)
<a href="home/category/{!! $cat->id !!}"> {{ $cat->name }} </a>
@endforeach
</div>
url in browser
http://localhost:8888/ko/public/home/category/6
and i'm getting NotFoundHttpException in RouteCollection.php line 161
Does anyone see something wrong ?
I have another route which is working
Route::get('/home/project/{project_id}','ProjectDetailsController@index');
Thanks for help
here is more code
routes.php
Route::auth();
Route::get('/', 'HomeController@index');
Route::get('/home', 'HomeController@index');
Route::get('/home/project/{project_id}','ProjectDetailsController@index');
Route::post('/home/create/addProject', 'CreateController@addProject');
Route::get('/home/create', 'CreateController@index');
Route::get('/home/category/{$category_id}', 'HomeController@category');
HomeController.php
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct(ProjectRepository $project, UserRepository $user, CategoryRepository $category)
{
//$this->middleware('auth');
$this->project = $project;
$this->user = $user;
$this->category = $category;
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$listProject = $this->project->getAllProjects();
$categories = $this->category->getAllCategories();
foreach($listProject as $key => $project){
// get creator of project
$user[$key] = $this->user->getCreator($project->creator_id);
// get category of project
$category[$key] = $this->category->getCategory($project->category_id);
}
return view('home', [
'projects' => $listProject,
'user' => $user,
'category' => $category,
'categories' => $categories
]);
}
public function category(Request $request, $category_id)
{
$listProject = $this->project->getProjectsFromCategory($category_id);
return view('category', [
'projects' => $listProject,
]);
}
}
view
@extends('layouts.app')
@section('content')
<div class="container-fluid">
<div class="row">
@foreach ($projects as $key => $project)
<div class="col-md-4" style='margin-bottom:20px;'>
<div class="border" style="border:1px solid #e7e7e7; padding:15px;">
ID : {{ $project->id }} <br>
TITLE : {{ $project->title }} <br>
CATEGORY : {{ $category[$key]->name }} <br>
CREATOR : {{ $user[$key]->name }} <br> <br>
DETAILS : <a href="home/project/{{ $project->id }}">See project</a>
</div>
</div>
@endforeach
</div>
<div class="row">
<div class="col-md-12">
@foreach ($categories as $key => $cat)
<a href="home/category/{{ $cat->id }}"> {{ $cat->name }} </a>
@endforeach
</div>
</div>
</div>
@endsection
$
from the statementRoute::get('/home/category/{$category_id}', 'HomeController@category');
in the routes. Change it toRoute::get('/home/category/{category_id}', 'HomeController@category');
– linuxartisanRoute::get('/home/category/{category_id}', 'HomeController@category');
and get the urlhttp://localhost:8888/ko/public/home/category/6
you will not be getting this error NotFoundHttpException in RouteCollection.php. And you don't have$category
variable defined incategory()
method in the controller. – Sanzeeb Aryal