I am using Laravel 5.3, the task that i am currently working on is the Form routing.
This is my routes.php file.
Route::group(['middleware' => 'web'], function() {
Route::get('/login', ['as' => 'login', 'uses' => 'LoginController#login']);
Route::post('/handleLogin', ['as' => 'handleLogin', 'uses' => 'LoginController#handleLogin']);
});
The actual Form code in the view.
{!! Form::open(array('route' => 'handleLogin')) !!}
<div class="form-group">
{!! Form::label('email') !!}
{!! Form::text('email', null, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::password('password', array('class' => 'form-control')) !!}
</div>
{!! Form::token() !!}
{!! Form::submit('Login', array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
The controller that has the handle function.
/* handleLogin function to request the data*/
public function handleLogin(Request $request){
$data = $request-> only('email', 'password');
if(\Auth::attempt($data)){
return 'Is Logged In';
return redirect()-> intended('/home');
}
return back()->withInput();
}
When i click on Login button, a blank page is displayed instead of the page that would display 'Is Logged In'.
Any help would be appreciated.
You should remove web middleware from routes file since you're using Laravel 5.3 in which web middleware is added automatically. Adding it manually will cause problems.
Related
Im trying to use a form to submit reviews for products but I believe the submit button uses the incorrect controller store method. I have a controller for products and one for reviews. The products store works correctly and I can see the database being populated once submitted however when I go to submit a review for a product it will throw the custom error messages from the product store form. If I change the reviews form::open to the products form::open it will throw an error: The PUT method is not supported for this route. Supported methods: GET, HEAD, POST.
Products form (works properly)
{!! Form::open(['action' => 'App\Http\Controllers\ProductsController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
... labels and text ...
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
Reviews form
<div>
<p>Write a review</p>
<!-- submit review form -->
{!! Form::open(['reviews' => 'App\Http\Controllers\ReviewsController#store']) !!}
<div class="form-group">
{{ Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Write your message']) }}
</div>
<div class="form-group">
{{ Form::label('rating', 'Rating') }}
{{ Form::select('rating', ['1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5'], '1') }}
</div>
{{ Form::hidden('_method', 'PUT') }}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
</div>
ReviewsController store
public function store(Request $request, $id)
{
$this->validate($request, ['description' => 'nullable',
'rating' => 'nullable',
]);
$review = new Review;
$review->rating = $request->input('rating');
$review->reviewerid = auth()->user()->id;
$review->productid = $id;
$review->description = $request->input('description');
$review->save();
return redirect('/products/$id')->with('success', 'Review submitted');
}
Web.php file
Route::get('/', 'App\Http\Controllers\PagesController#index');
Route::get('/about', 'App\Http\Controllers\PagesController#abouts');
Route::get('/cart', 'App\Http\Controllers\PagesController#cart');
Route::get('/checkout', 'App\Http\Controllers\PagesController#checkout');
Route::get('/dashboard', 'App\Http\Controllers\PagesController#services');
Route::get('/categories/{Category}', 'App\Http\Controllers\PagesController#category');
Route::resource('reviews', 'App\Http\Controllers\ReviewsController');
Route::resource('products', 'App\Http\Controllers\ProductsController');
Auth::routes();
The error is because of this line:
Form::hidden('_method', 'PUT')
You're telling laravel to use put method. Delete it and it will work fine.
For your form action, I think you have type in this line:
{!! Form::open(['reviews' => 'App\Http\Controllers\ReviewsController#store']) !!}
Change it to :
{!! Form::open(['action' => 'App\Http\Controllers\ReviewsController#store']) !!}
Why am I getting this error?
Routes:
Route::group(['prefix'=>'admin','middleware'=>'auth'],function(){
Route::get('/',['uses'=>'Admin\IndexController#index','as'=>'adminIndex']);
Route::resource('/products','Admin\ProductController');
});
Form:
{!! Form::open(['url' => route('admin.products.edit',['products'=>$product->id]),'class'=>'form-horizontal','method'=>'POST']) !!}
{{ method_field('EDIT')}}
{!! Form::button('Edit', ['id'=>'submit','type'=>'submit']) !!}
{!! Form::close() !!}
Also, when I'm trying to get list of routes by typing php artisan route:list, I'm getting error:
[Symfony\Component\HttpKernel\Exception\HttpException]
What's the problem?
I think your resource under admin prefix does not take the admin prefix. So you still have to use it without admin prefix. Besides, you are using 'url' instead of 'route'.
{!! Form::open(['url' => '/admin/products/'.$product->id.'/edit'),'class'=>'form-horizontal','method'=>'POST']) !!}
If you want to keep using route
{!! Form::open(['route' => ['products.edit', 'products'=>$product->id], 'class'=>'form-horizontal','method'=>'POST']) !!}
If you are tryint to update instead
{!! Form::model($product, ['method' => 'PATCH', 'route' => ['products.update', $product->id], 'class' => 'form-horizontal' ]) !!}
Since edit method uses GET, change your code to:
{!! Form::open(['route' => ['admin.products.edit', $product->id], 'class' => 'form-horizontal', 'method' => 'GET']) !!}
{!! Form::submit('Edit', ['id' => 'submit']) !!}
{!! Form::close() !!}
Also, since it's GET, you can use simple link:
<a href="{{ route('admin.products.edit', $product->id) }}">
<button class="btn" id="submit">Edit</button>
</a>
look down should be use route 'as'=>'admin.products.edit' and get id also redirect to controller function edit products
Route::group(['prefix'=>'admin','middleware'=>'auth'],function(){
Route::get('/edit/{id}/product',
[
'uses'=>'Admin\IndexController#index',
'as'=>'admin.products.edit'
]);
Route::resource('/products','Admin\ProductController');
});
I am trying to validate the login form and display errors on the page. But i am getting a 422 error when i click on login.
Any help would be appreciated.
This is my handleLogin function that handles the processes when login button is clicked.
public function handleLogin(Request $request){
/* validate user */
$this->validate($request, User::$login_validation_rules);
$data = $request->only('email', 'password');
if(\Auth::attempt($data)){
return redirect()->route('home');
}
return back()->withInput();
}
The following is the routes.php
Route::get('/login', ['as' => 'login', 'uses' => 'LoginController#login']);
Route::post('/handleLogin', ['as' => 'handleLogin', 'uses' => 'LoginController#handleLogin']);
Route::get('/home', ['as' => 'home', 'uses' => 'UsersController#home']);
Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginController#logout']);
Route::resource('users', 'UsersController', ['only' => ['create', 'store']]);
And the following is the login form.
#extends('master')
#section('content')
<h2> Login </h2>
#if(count($errors))
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{!! Form::open(array('route' => 'handleLogin')) !!}
<div class="form-group">
{!! Form::label('email') !!}
{!! Form::text('email', null, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::password('password', array('class' => 'form-control')) !!}
</div>
{!! Form::token() !!}
{!! Form::submit('Login', array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
#stop
Today I have the following problem with this routes , It has never happened to me before now.
{!! Form::open(array('route' => 'subastas/creado', 'class' => 'form')) !!}
<div class="form-group">
{!! Form::label('Your Name') !!}
{!! Form::text('name', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your name')) !!}
</div>
<div class="form-group">
{!! Form::label('Your E-mail Address') !!}
{!! Form::text('email', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your e-mail address')) !!}
</div>
<div class="form-group">
{!! Form::label('Your Message') !!}
{!! Form::textarea('message', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your message')) !!}
</div>
<div class="form-group">
{!! Form::submit('Contact Us!',
array('class'=>'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
In my route controller
Route::post('subastas/creado', array(
'as' => 'subastas/creado',
'uses' => 'SubastaController#creado'
));
My controller
public function creado()
{
$usuario = new Subasta();
$usuario->name= \Request::input('name');
$usuario->save();
}
When I send the form I recieve this url ? Any idea about this problem ?
http://localhost/laravel30/public/subastas/create?_token=X93VGoFhFL9YaPYZfrTlyvn0ph9KE6Om00KmMaiv&name=asdafs&email=kfh1992%40gmail.com&message=
I assume you have another route of subastas/creado for the GET request to display the form.
In your Form::open() you're using that to generate the URL, laravel is seeing that as a GET route as thats the first one registered in your routes.php and changing the form method to GET rather than the expected POST
The solution is to change the name of the route and use that in your Form::open()
Route::post('subastas/creado', [
'as' => 'subastas/creado/post',
'uses' => 'SubastaController#creado',
]);
Then you can use the following to generate the correct form opening tag.
Form::open(['route' => 'subastas/creado/post'])
I'm not sure why it's not working...
I'm trying to update a user and I keep getting method not allowed error exception.
-- routes
Route::get('superadmin/users', ['as' => 'superadmin.users', 'uses' => 'SuperAdminController#usersIndex']);
Route::post('superadmin/users/{id}', ['as' => 'superadmin.editUser', 'uses' => 'SuperAdminController#editUser']);
-- controller
public function usersIndex()
{
$users = User::all();
return View::make('superadmin.users',compact('users'));
}
public function editUser($id)
{
$user = User::findOrFail($id);
$user->email = Input::get('email');
$user->save();
return Redirect::route('superadmin.users')->with('alertsuccess', 'User has been updated.');
}
-- view
{{ Form::model($user, ['method' => 'PATCH', 'route' => ['superadmin.editUser', $user->id], 'class' => 'form']) }}
<div class="form-group">
{{ Form::label('email', 'Email:', ['class' => 'placeholder-hidden']) }}
{{ Form::text('email', Input::old('email'), ['class' => 'form-control']) }}
</div>
{{ Form::submit('Update User', ['class' => 'btn btn-primary']) }}
{{ Form::close() }}
This is most probably as you need to set up a resource controller in order to use the PATCH method. Try using POST instead.