My Form tag is
<form action="<?php echo route('profile.store'); ?>" method="post">
<input type="hidden" name="_token" id="csrf-token" value="<?php echo csrf_token(); ?>" />
and my web.php file have
Auth::routes();
Route::view('/','welcome');
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/profile','ProfileController#index');
Route::get('/profile/add','ProfileController#create');
my ProfileController have store function
public function save(Request $request){
print_r($request);
}
First, you don't have the store method in your ProfileController, instead of that, you are using save method. So, you can do that.
Create a new route for your save method in your web.php
Route::post('profile/save', ProfileController#save)->name('profile.store');
Then, your final code will be:
In your view:
<form action="<?php echo route('profile.store'); ?>" method="post">
<input type="hidden" name="_token" id="csrf-token" value="<?php echo csrf_token(); ?>" />
In your controller:
public function save(Request $request){
print_r($request);
}
In your web.php
Route::post('profile/save', ProfileController#save)->name('profile.store');
First you need to define the route in your web.php, and secondly you need to name a route as profile.store.
https://laravel.com/docs/master/routing#named-routes
If you use the resource function then the routes are named already.
https://laravel.com/docs/5.0/controllers#restful-resource-controllers
You can see the list of available routes using php artisan command.
php artisan route:list
More help about this command:
http://laravel-school.com/posts/laravel-php-artisan-route-list-command-9
You don't have route named profile.store.
Try changing the form tag from
route('profile.store');
to this
action('ProfileController#store');
Note: I am assuming you have that route (as you have not shown it in the question)
Route::post('/profile','ProfileController#store');
Related
I'm setting up my custom authentication system in my Laravel app. I've deleted all the default auth controllers and not using make::auth. And my auth is working properly. My main problem is that when I tried to log in for the first time, it's failing with "Route [login] not defined" error, but in second attempt, it's working properly. And if I repeat the process, it's continuing again and again like the first two attempt. Actually, I've never used login route anywhere.
Here is my form:
<form action="{{ url('/log-in') }}" method="POST">
#csrf
<input type="text" name="phone" placeholder="Telefon" class="form-control input-phone">
<input type="password" name="password" placeholder="Parol" class="form-control">
<button type="submit" class="btn">Kirish</button>
</form>
Here is my route:
Route::post('/log-in', 'AuthController#login');
Here is my controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use App\User;
class AuthController extends Controller
{
public function login(Request $request) {
// Get current user.
$user = User::where('phone', $request->phone)
->first();
if ( Hash::check($request->password, $user['password']) ) {
Auth::login($user, true);
Auth::logoutOtherDevices($request->password);
return redirect()->back();
}
}
}
Use for route.
Route::post('/log-in', 'AuthController#login')->name('login');
Use your form.
<form action="{{ route('login') }}" method="POST">
#csrf
<input type="text" name="phone" placeholder="Telefon" class="form-control input-phone">
<input type="password" name="password" placeholder="Parol" class="form-control">
<button type="submit" class="btn">Kirish</button>
</form>
This is a tricky and annoying problem. you need to change your return from return redirect()->back(); to loading 1 known blade or a known redirect. Sometimes at login your route fails to set a back redirect. so you can try to set a return view() or a return to a known url. So for example if the login is success return to index , if not load error page.
Hope this helps
I am following laravel tutorial by jeffrey. But I have got a problem with while update the form. I'm getting all form fields but when updating it is showing that page not found. I have tried many multiple what jeffrey told how to updating form in 5.2
https://laracasts.com/series/laravel-5-from-scratch
<form action="form/{{$user->id}}" method="POST">
{{method_field("PATCH")}}
<label for="name">Name</label>
<input type="text" name="name" value="{{$user->name}}">
<label for="email">Email</label>
<input type="text" name="email" value="{{$user->email}}">
<label for=""></label>
<input type="submit" value="Submit" name="submit">
</form>
this is routes
Route::get("form/{id}/edit", "you#edit");
Route::patch("form/{user}", "you#update");
This is my controller
public function edit($id)
{
$user = laravel::findorfail($id);
return view("form", compact("user"));
}
public function update(Request $request, User $user)
{
$user->update($request->all());
}
Thank you in advance.
Try to run php artisan route:clear.
You also need to add field with CSRF token. Here's an example from the documentation:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
As I see this, the problem with the route to be form/form/{id} is because of your action route of the form. To avoid this add / before the path, then, the final path will be /form/{id}. Another way you can get this is just using deleting the form/ words from you path and just keeping the {id}.
Explaining a little about what this is happening when you add / and the beginning of a route you are creating an absolute path, but if you start the path without the / then you are using relative path, this means that the path will depend on the current app url.
The best way to avoid this sort of things is using named routes. I always try to use them.
Eventually I found out the solution.
The routes patch should be:
Route::patch("form/{user}", "you#update");
but actually it should be like:
Route::patch("form/form/{id}", "you#update");
and the update controller should be like:
public function update(Request $request, $id)
{
$user = laravel::find($id);
$user->update($request->all());
return "sucess";
}
I get this error (when I try to subscribe to a monthly newsletter)
Class 'App\NewsLetterManager' not found
When I have this website running locally (on vagrant) it all works fine, but when I transfer my project to the online server I get the error above.
In my Controller I DO use the NewsLetterManager.. So no idea what is going on.
Controller (snippet)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\NewsLetterManager;
class HomeController extends Controller
{
..
public function addToMailingList(Request $request)
{
$this->validate($request, [
'email' => 'required|email|max:195'
]);
$newsLetterManager = new NewsLetterManager($mailchimp = app('Mailchimp'));
$newsLetterManager->addEmailToList($request->email);
return Redirect::to('/')->with('message', 'mailing');
}
..
}
Blade
<form method="post" action="{{ route('mailing.add') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input class="bordered-input" type="email" name="email" placeholder="Subscribe here" />
</form>
Routes
Route::post('/mailing/add', ['as' => 'mailing.add', 'uses' => 'Controller#addToMailingList']);
Thanks guys
I think you have to upload your up-to-date vendor folder into the server or update your composer in the server.
i want to pass two variables in view page
my code.
$user=$this->session->userdata('username');
$fyear=$this->session->userdata('fyear');
echo form_open('Erp_c/usercustomerinsert/'.$user);?>
I want to pass the other variable $fyear too..
I tried this.
<?php
$user=$this->session->userdata('username');
$fyear=$this->session->userdata('fyear');
echo form_open('Erp_c/usercustomerinsert/'.$user.$fyear);?>
value is not getting.
Try this
<?php
$user=$this->session->userdata('username');
$fyear=$this->session->userdata('fyear');
$add = $user.$fyear;
echo form_open('Erp_c/usercustomerinsert/'.$add);
?>
and in config/autoload.php
$autoload['helper'] = array('url','form','html');
You can pass it using by using hidden variable
$user=$this->session->userdata('username');
$fyear=$this->session->userdata('fyear');
$attributes = array('user' => $user, 'fyear' => $fyear);
echo form_open('Erp_c/usercustomerinsert/','', $attributes);
The above code make you form like
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/Erp_c/usercustomerinsert/">
<input type="hidden" name="user" value="user_VALUE" />
<input type="hidden" name="fyear" value="fyear_VALUE" />
Hello so I am trying to make an update form in Laravel using plain html. The url of my edit form is http://localhost:8000/profile/sorxrob/edit, sorxrob is the username. And the code in that url is:
<form class="form-horizontal" role="form" action="" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
//more inputs
</form>
And in my controller named AccountController:
public function update(Request $request, $id)
{
$account = Accounts::findOrFail($id);
$input = $request->all();
$account->fill($input)->save();
return 'success';
}
And I am getting the error MethodNotAllowedHttpException when I click the update button. Is it because my action is equal to nothing? If yes, what is the correct way of routing there?
This is due to your action url which is not correct routing url. Use the following
(1) First define a route in your route.php file
Route::post('profile/{username}/edit', array('as' => 'profile.update', 'uses' => 'AccountController#update'));
(2) Change your action attribute from your form tag
action="{{ URL::route('profile.update', [$username]) }}"
here $username variable will be passed from your AccountController#edit method.