0
votes

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
1
Try removing the $ from the statement Route::get('/home/category/{$category_id}', 'HomeController@category'); in the routes. Change it to Route::get('/home/category/{category_id}', 'HomeController@category');linuxartisan
if you replace Route::get('/home/category/{category_id}', 'HomeController@category'); and get the url http://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 in category() method in the controller.Sanzeeb Aryal
Thanks ! The error was that linuxartisan & Sanzeeb pointed out. A simple $, made me struggle for hours.. lol..Lucas Dmnt

1 Answers

0
votes

Your controller is wrong. You send to controller from route only 1 parameter ($category_id). But accept another parameter: $category_name in your controller.

Removing $category_name from controller will fix an error.

You need just define only parameters from route in your controller. /category is not a parameter.

Documentation

Also there is a wrong code in your blade file:

Replace

<a href="home/category/{!! $cat->id !!}"> {{ $cat->name }} </a>

With

<a href='{{url("home/category/$cat->id")}}'> {{ $cat->name }} </a>