2

I have difficults to create a supplementing Resource in a controller laravel and insert it in the web.php for the routing.

I would to implement a search functions, in an Articles controller .Beyond the authomatic generated function like (index, show...) i have created another one:

public function search($title){ $articles = Article::findOrFail($title); return $articles; }

And I have added it in the web.php for the routing:

Route::resource('articles/{title}', 'ArticleController@search'); 

When I try to test this search, it doesn't work. All the implicit controller go well, I have problem only with this selfmade function.

How can I solve this issue?

thanks

1
  • Resource routes have to point to an (entire) resource controller, implementing the functions described in the docs. This way you try to point to 1 function search. Commented Dec 7, 2017 at 15:35

1 Answer 1

1

You need to add another route before resource one to make it work:

Route::get('articles/{title}', 'ArticleController@search'); 

You also want to change the query if you want to find an article by it's slug, for example:

$article = User::where('slug', $title)->first(); 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot! Two mistake: the get instead resource in the route and the query! Thanks
@Alexey Mezenin Maybe you can help me. Look at this : stackoverflow.com/questions/49594340/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.