1

I'm new to laravel and still learning about this framework. I already found some questions on stackoverflow but it still didn't work out for me.

My problem is:

I got this localhost/codehub/public/users/create

and the route:

Route::get('users/create',['uses' => 'UserController@create']); 

Inside the page there's some form like this:

my page inside

so when I click create button it is supposed to route it into store function in the user controller

Route::post('users',['uses' => 'UserController@store']);

public function store(Request $request)
{
    return $request->all();
}

so the problem is when I click that create button it always redirects me to localhost/users and because of that, I can't process my store function. Any advice?

this is my form code:

<form method="post" action="/users">
    <input type="text" name="name">
    <input type="email" name="email">
    <input type="password" name="password">
    <input type="submit" value="Create">
</form>
6
  • First of all create virtual host. After that check in .env file APP_URL. After that why you didnt use Route::post('users' 'UserController@store') syntax ? Commented Oct 25, 2016 at 14:29
  • @VaheGalstyan hi sir, hmm what do you mean about create virtual host ? i checked in my APP_URL in .env : APP_URL=localhost Commented Oct 25, 2016 at 14:32
  • laravel-recipes.com/recipes/25/creating-an-apache-virtualhost this is link for creating virtual host, after creating change your hostname in .env APP_URL=hostname Commented Oct 25, 2016 at 14:34
  • @VaheGalstyan hi sir , hmm is it necessary to creating virtual host ? i already try to find the folder /etc/apache2/sites-available , is it in my xampp / apache folder ? Route::post('users' 'UserController@store') i already try using this instead Route::get('users/create',['uses' => 'UserController@create']); still getting same problem , always object not found and redirect to localhost/users Commented Oct 25, 2016 at 14:45
  • It's not necessary, but it good practice. This link is for xamp delanomaloney.com/2013/07/10/…. Route::post('users', 'UserController@store') between users and UserController. Commented Oct 25, 2016 at 15:16

1 Answer 1

1

The problem may be because of relative path in form action.

You should always use named routes which allow the convenient way of generation of URLs or redirects for specific routes. So you can change your route as:

Route::post('users', 'UserController@store')->name('users.create');

And in form you can write as:

 <form method="post" action="{{ route('users.create') }}">
Sign up to request clarification or add additional context in comments.

5 Comments

thanksss sirr !!! it worked !! , if u dont mind , may i ask u what method that can we use for declaring action ?
Happy to help :D... I didn't get your question. Can you explain it a little bit more?
as u said that i should always use named routes , since im using direct path like this "/users" . is there any method for declaring action in laravel beside these 2 (used named routes , using direct path) ? thanks you very much sir !
There are others methods to do this but not specifically in Laravel. You have to use the package LaravelCollective/html and have look here. And for the reason, I am saying to use named route is because imagine a case where you have to change the user create URL to users/something_new then you have to go and change it in every view file whereas if use named route then you to change it in only route file
ohh okayy sirr ! thank you very much sir for answering my many question ! thank you for ur time !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.