user3592644
user3592644

Reputation:

laravel NotFoundHttpException

I am new at laravel. I am trying to make a link to another page. I have the page index and want to go to desc which display information about a vehicle selected in index page. The problem is that it shows the error:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

index.blade.php

    @foreach ($cars as $car)
           <tr>                                   
           <td> 
           {{link_to_action('CarController@show',  $car->Description, $car->id)}}</td>  
             {{ Form::open(array('action' => 'CarController@show', $car->id)) }}
                  {{ Form::close() }}
                        <td>{{ $car->License }}</td>  
                        <td>{{ $car->Milage }}</td> 
                        <td>{{ $car->Make }}</td>  
                        <td>{{ $car->status }}</td>                                    
          </tr>                                                            
    @endforeach

routes.php

Route::resource('/', 'CarController');
Route::resource('create', 'DataController');
Route::post('desc', array('uses' => 'CarController@show'));
Route::post('create', array('uses' => 'CarController@create', 'uses' => 'DataController@index'));
Route::post('update', array('uses' => 'CarController@update'));
Route::post('store', array('store' => 'CarController@store'));

Upvotes: 15

Views: 83163

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87779

The 'NotFoundHttpException' means Laravel wasn't able to find a route to for the request.

Your desc route is a POST route, only, and a link_to_action will create a GET request, so you may need to change add a GET route too:

Route::post('desc', array('uses' => 'CarController@show'));
Route::get('desc', array('uses' => 'CarController@show'));

There's also a any, which does GET, POST, PUT, DELETE:

Route::any('desc', array('uses' => 'CarController@show'));

If you need to get and id from your route, you will have to add it as a parameter:

Route::post('car/{id}', array('uses' => 'CarController@show'));

And you will have to access your page as:

http://myappt.al/public/car/22

But if you want to access it as:

http://myappt.al/public/22

You'll need to do:

Route::post('{id}', array('uses' => 'CarController@show'));

But this one is dangerous, because it will may grab all the routes, so you MUST to set it as your very last route.

And your controller must accept that parameter:

class CarController extends Controller {

   public function show($id)
   {
      dd("I received an ID of $id");
   }
}

EDIT:

Since you most of your routes made manually, you can also go with the index this way:

Route::resource('create', 'DataController'); 

Route::get('/', 'CarController@index');

Route::post('create', array('uses' => 'CarController@create','uses' => 'DataController@index')); 
Route::post('update', array('uses' => 'CarController@update')); 
Route::post('store', array('store' => 'CarController@store')); 

Route::get('{id}', array('uses' => 'CarController@show'));
Route::post('{id}', array('uses' => 'CarController@show'));

Upvotes: 19

Related Questions