Laravel 5 input old is empty - php

My routes is here
Route::get('sign-up', ['as' => 'signUp', 'uses' => 'UserController#signUpGet']);
Route::post('sign-up', ['as' => 'signUpPost', 'uses' => 'UserController#signUpPost']);
Controller
return redirect('signUp')->withInput();
And View
<form role="form" method="POST" action="{{route('signUpPost')}}">
<input type="text" class="form-control" name="username" value="{{ old('username') }}">
</form>
The {{old()}} function return empty value.
EDIT
I took
NotFoundHttpException in RouteCollection.php line 145:

Your problem looks like you are not actually submitting the username in the first place:
<form role="form" method="POST" action="{{route('signUpPost')}}">
<input type="text" class="form-control" name="username" value="{{ old('username') }}">
</form>
There is no 'submit' button inside the form. If you submit outside the form - then the username will not be included.
Add the submit button inside your form - then try again
<form role="form" method="POST" action="{{route('signUpPost')}}">
<input type="text" class="form-control" name="username" value="{{ old('username') }}">
<input type="submit" value="Submit">
</form>
Edit - also your controller is wrong. It should be this:
return redirect()->route('signUp')->withInput();

All you are missing is to Flash the Input to the session. This is so it's available during the next request.
$request->flash();
Do that just before calling to View your form.
Source: http://laravel.com/docs/5.1/requests#old-input

<div class="form-group #if($errors->first('username')) has-error #endif">
<label for="username" class="rtl">Enter user name </label>
<input type="text" name="username" class="form-control rtl basic-usage" id="username" placeholder="Enter user name" value="{!! old('username') !!}">
<span class="help-block required">{{$errors->first('username')}}</span>
</div>
above form field will show old value entered.
will show validation error (you have to specify error separately.)
have a place holder.
bootstrap to look better.(add bootstrap)

Your name in the register view should correspond with keys in create() and validate() method inside your RegisterController file.

My problem here was caused by "data-prefill" in the input. Once I removed this, it worked.

You can try this: {{ Input::old('username') }}.

Related

Laravel form submission doesn't goes to the right route?

I have installed the latest laravel. I Have made this simple form. I want to create post but when I submit it goes to localhost/post which is the wrong URL . The actual URL is http://localhost/laravel_practice/'
Form
<form method="post" action="/post">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control" placeholder="Enter Title Here">
</div>
<div class="form-group">
<label>Body</label>
<textarea name="body" class="form-control" placeholder="Enter the body"></textarea>
</div>
<div class="form-group">
<input type="submit" name="sumit" class="btn btn-primary" value="Publish">
</div>
My Routes
Route::get('/' ,'PostController#index');
Route::get('/posts/create', 'PostController#create');
Route::post('/post','PostController#store');
Your short fix is to use action="/laravel_practice/post" or action="/laravel_practice/public/post" depending on what url you want to go.
However, it is a bad practice. You should use route name. To do that give any name to the route like below,
Route::post('/post','PostController#store')->name('post.store');
Then in view you should use,
<form method="post" action="{{ route('post.store') }}">
To read more about named route you can go through this document.

Laravel Resource default method - update

I have a controller called ItemController. With this controller, the method store and index are the only recognized methods. when i use the method update, it throws an error
Missing required parameters for [Route: items.update]
Route.php
Route::resource('items', 'ItemsController');
ResourceRegistrar.php
protected $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];
html
<form role="form" method="post" action="{{ route('items.update') }}">
{!! csrf_field() !!}
<div class="form-group" style="width: 400px;">
<label for="exampleInputPassword1">Item Name</label>
<input type="text" class="form-control" id="exampleInputPassword1" name="name" value="{!! $item->name !!}" placeholder="item name" required="">
</div>
<div class="form-group" style="width: 400px;">
<label for="exampleInputPassword1">Retail Price</label>
<input type="number" step="any" class="form-control" id="exampleInputPassword1" value="{!! $item->retail_price !!}" name="retail_price" placeholder="retail price" required="">
</div>
<div class="form-group" style="width: 400px;">
<label for="exampleInputPassword1">Quantity Price</label>
<input type="number" step="any" class="form-control" id="exampleInputPassword1" value="{!! $item->quantity_price !!}" name="quantity_price"
placeholder="quantity price " required="">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Controller
public function update(Request $request, $id)
{
$item = Item::findorFail($id);
$item->name = $request->get('name');
$item->retail_price = $request->get('retail_price');
$item->quantity_price = $request->get('quantity_price');
$item->update($request, $id);
Session()->flash('flash_message', 'Item successfully updated');
return redirect()->route('items.index');
}
How is this happening?
When you call items.update route it expects the item you are updating, but you are not passing that item in the request, so the route throws an error because you are missing a required item to be able to know which one you are updating, does this make sense?
EDIT:
you need to include $item->id in your route('items.update', $item->id)
You need to be including a route parameter for the item you are updating. If you run
php artisan route:list
you should see something like: /items/{item} App\Http\Controllers\ItemController.
Your request URL should include the item id like:
`/items/1`
where 1 is the primary key for the item updating.
edit
You need to pass id to the route helper method in your HTML form:
action="{{ route('items.update', $item->id) }}"
Also, in your form, set the method as PUT or PATCH by:
<form>
{!! method_field('put') !!}
//or
{!! method_field('patch') !!}
...
</form>
Laravel uses a technique called Form Method Spoofing to fake PUT, PATCH, and DELETE HTTP verbs via POST.
edit 2
You're saving the record incorrectly, change
$item->update($request, $id);
to
$item->save();

Whoops, looks like something went wrong. LARAVEL ERROR

I wanted to make a basic login/logout in Laravel. So I created a new folder under resources/views called auth and then I made a new file login.blade.php inserted this into it:
<html>
<body>
<form>
<input type="text" name="email" placeholder="email" size="40"><br><br>
<input type="password" name="password" placeholder="password" size="40"><br><br>
<input hidden name="_token" value="{{csrf_token}}">
<input type="submit" value="send">
</form>
</body>
</html>
After that I edited the web.php like this:
Route::get('/', function () {
return view('welcome'); });
Route::get('/home', function () {
return view('welcome'); });
Route::get('/login', function () {
return view('auth.login'); });
Route::post('/login','Auth\LoginController#login');
Route::post('/logout','Auth\LoginController#logout');
So it should work fine because everything makes sense but whenever I goto login url, I see this error message:
ErrorException in 6c95db2d362954448afd30aa9a2bf2cb0fc31937.php line 6:
Use of undefined constant csrf_token - assumed 'csrf_token' (View: G:\xampp\htdocs\o2architect\root\laravel\resources\views\auth\login.blade.php)
So can anyone tell me whats going on here ?!
Change it:
<input hidden name="_token" value="{{csrf_token}}">
to
<input hidden name="_token" value="{{ csrf_token() }}">
and try again.
Try below, Hope this work for you!
<html>
<body>
<form>
<input type="text" name="email" placeholder="email" size="40"><br><br>
<input type="password" name="password" placeholder="password" size="40"><br><br>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" value="send">
</form>
</body>
</html>
The csrf_token is a helper function of Laravel so you will have to call it with parentheses.
Just Change
<input hidden name="_token" value="{{csrf_token}}">
to
<input hidden name="_token" value="{{ csrf_token() }}">
Recommendation
You can use {!! csrf_field() !!} which will create hidden input field with the CSRF Token.
In the code above the form that you have shown is a GET Request form, you will have to change it to action="POST" and GET request forms work without CSRF Token also.

How to update a Table from Form data in Laravel 5

I have a form in Laravel5
<form method="POST" action="http://localhost:8000/song/Baby/update" accept-charset="UTF-8">
<input name="_method" type="hidden" value="PATCH">
<input name="_token" type="hidden" value="kagIHsGe3zOZSPVyW6wW84Cn5eresZ2nlF287nNK">
<div class="form-group">
<input class="form-control" name="title" type="text" value="Baby">
</div>
<div class="form-group">
<textarea class="form-control" name="lyrics" cols="50" rows="10">
Yo Yo Yo BABY
</textarea>
</div>
<div class="form-group">
<input type="submit" value="Update Song">
</div>
</form>
Then in Route file I have written the code
patch('songs/Baby/update','SongsController#update');
Its throwing error
Sorry, the page you are looking for could not be found.
NotFoundHttpException in RouteCollection.php line 143:
Is the syntax changed for PATCH request in Laravel 5?
Your route and form action are different.
You have defined a route with songs (plural) and used as song (singular) in form action.
Try changing your form action to
action="http://localhost:8000/songs/Baby/update"
Try this: <input type="hidden" name="_method" value="PUT"> and Route::put('songs/Baby/update','SongsController#update').

Form does not submit in laravel 5

I want to submit a form in laravel 5 but it does not call the update function.
<form class="form-horizontal", role="form" method="patch" action{{url('/user/'.$user->id) }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="name" value="{{ $user- >name }}">
</div>
</div>
</form>
The action attribute is not complete, should be action="..."
You can also use route instead of url e.g.
In blade:
<form class="form-horizontal", role="form" method="patch" action="{{ route('user.show') }}">
In routes.php:
Route::get('user/{id}', [
'uses' => 'UsersController#action',
'as' => 'user.show'
]);
The action is incorrect. Missing = and beginning ". You also have a , after class. Not valid html.
If you want to pass parameters to an URL then maybe you should consider this.
url('foo/bar', $parameters = [], $secure = null);
Your html is invalid, you are missing the action attribute. HTML forms should generally take the form:
<form action="where/form/submits/to" method="form-method"> ... </form>
see this for details on the form element.
In your case that will is should be like:
<form class="form-horizontal", role="form" method="patch" action="{{url('/user/'.$user->id) }}"> ... </form>

Categories