1
votes

I have just followed a tutorial where the only code I needed in web.php to call a controller function was:

Route::resource('/articles', 'ArticleController');

This works for all my functions that were already there when creating the project (edit, update, destroy,...) Now I need to make a second update function (upvote). But when creating a new function in the controller and routing to it I get the following error:

Route [articles.upvote] not defined. (View: D:\school\web-backend\Oplossingen\hackernews\resources\views\articles\index.blade.php)

How do I make the "a" tag go to the upvote function of the controller? My code:

public function upvote(Request $request, $id)
{ /* ArticleController.php */
    $article = Article::find($id);
    $article->points += 1;
    $article->save();
    Session::flash('succes', 'Upvote was a succes');

    return redirect()->route('articles.index');
}

td> <!-- index.blade.php !-->
                           <a href="{{ route('articles.upvote', ['articles'=>$storedArticle->id]) }}" class="btn btn-default">upvote</a>
                        </td>
                        <td>downvote</td>
                        <td>
                            <a href="{{ route('articles.edit', ['articles'=>$storedArticle->id]) }}" class="btn btn-default">edit</a>
                        </td>


Route::get('/', function () { /*route\web.php */
return view('welcome');

});

Route::resource('/articles', 'ArticleController');

2
Note: the version of Laravel I'm using is v5.3.29 - SdR

2 Answers

3
votes

You need to name your route if you want to use the articles.upvote syntax.

Your web.php file should look like this:

// Use 'GET' or 'POST' depending on how you have your view setup
Route::get('/articles/{article}/upvote', 'ArticleController@upvote')->name('articles.upvote');
Route::resource('/articles', 'ArticleController');

More information on routing can be found here: https://laravel.com/docs/5.3/routing#named-routes

0
votes

you need to add your upvote function in your route. so you have to create new route in your web.php like

Route::post('articles/{Article}/{article_id}/upvote','ArticleController@upvote')->name('upvote');