Laravel post request not works - laravel

I have Form in Laravel and when i submit the form to redirect another page("action = panel") with inputs's value. but problem is that when i enter in another's link it displays error. what is wrong?
This is form
this is another page when submit form
this is error when i enter in link again
this is form code:
<form action="{{route('adminPanel')}}" class="form" method="POST">
<p>Name:</p>
<input type="text" name="name"><br>
<p>Password:</p>
<input type="password" name="password"><br>
<input type="submit" name="submit" value="Enter As Admin" class="submit">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
this is routes:
Route::get('/', [
'uses' => 'AdminController#getAdminIndex',
'as' => 'index.admin'
]);
Route::post('/panel', [
'uses' => 'AdminController#getAdminPanel',
'as' => 'adminPanel'
]);
this is controller:
class AdminController extends Controller
{
public function getAdminIndex(){
return view('admin/index');
}
public function getAdminPanel(Request $request){
return view('admin/admin', ['name' => $request->name]);
}
}

this is because when you enter an address in address bar, your are actually sending a get request. but you've defined your route with post method!
to fix this you can use any:
Route::any('/panel', [
'uses' => 'AdminController#getAdminPanel',
'as' => 'adminPanel'
]);
and in controller:
use Illuminate\Support\Facades\Auth;
class AdminController extends Controller
{
public function getAdminIndex(){
return view('admin/index');
}
public function getAdminPanel(Request $request){
$name = $request->name ?: Auth::user()->name;
return view('admin/admin', ['name' => $name]);
}
}

Try to use the following statement in form as {{ csrf_field() }}

Sometimes routes may create you an issue. Try below snippet.
<form action="{{route('adminPanel')}}" class="form" method="POST">
<p>Name:</p>
<input type="text" name="name"><br>
<p>Password:</p>
<input type="password" name="password"><br>
<input type="submit" name="submit" value="Enter As Admin" class="submit">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Correct your Routes as below and try.
Route::post('/adminPanel', ['uses' => 'AdminController#getAdminPanel', 'as' => 'adminPanel' ]);

Related

Undefined property: Illuminate\Support\Facades\Request::$email

I am new to laravel here i am trying to make a simple registration and login form,Registration form register the user in the database and login form login the user,But here i am getting this error been trying to solve this issue for many hours looked into many resources but can't figure it out,Any help would be appreciated..Thanks
Error:Undefined property: Illuminate\Support\Facades\Request::$email
Blade
#extends("layouts.master")
#section('title')
My page
#endsection
#section('content')
<div class="row">
<div class="col-md-6">
<h3>Sign-Up</h3>
<form action="{{ route('signup') }}" method="post">
<div class="form-group">
<label for="email">Your email</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="first_name">First Name</label>
<input class="form-control" type="text" name="first_name" id="first_name">
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" type="password" name="password" id="password">
</div>
<button type="sumbit" class="btn btn-primary">sumbit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
<div class="col-md-6">
<h3>Login </h3>
<form action="{{ route('signin') }}" method="post">
<div class="form-group">
<label for="email">Your email</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" type="password" name="password" id="password">
</div>
<button type="sumbit" class="btn btn-primary">sumbit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
#endsection
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::post('/signup', [
'uses' => 'UserController#postSignUp',
'as' => 'signup'
]);
Route::post('/signin', [
'uses' => 'UserController#postSignIn',
'as' => 'signin'
]);
Route::get('/dashboard', [
'uses' => 'UserController#getdashboard',
'as' => 'dashboard'
]);
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Controller
namespace App\Http\Controllers;
use App\Http\Requests;
use App\User;
use App\UserTypes;
use Auth;
use Hashids;
use Redirect;
use Request;
use Hash;
class UserController extends controller
{
public function getdashboard()
{
return view('dashboard');
}
public function postSignUp(Request $request)
{
$email = $request->email;
$first_name = $request->first_name;
$password = bcrypt($request->password);
$user = new User();
$user->email = $email;
$user->first_name = $first_name;
$user->password = $password;
$user->save();
return redirect()->route('dashboard');
}
public function postSignIn(Request $request)
{
if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
}
This helps importing use Illuminate\Http\Request; instead of use Request;
use Illuminate\Http\Request;
class UserController extends controller{
}
Change your Controller Function
use Illuminate\Http\Request;
public function postSignUp(Request $request)
{
$email = $request->input('email');
$first_name = $request->input('first_name');
$password = bcrypt($request->input('password'));
$user = new User();
$user->email = $email;
$user->first_name = $first_name;
$user->password = $password;
$user->save();
return redirect()->route('dashboard');
}
public function postSignIn(Request $request)
{
if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}

Laravel routes, form won't submit data into the database

beginner trying to submit a basic form into the database using laravel. I know I should be using 'get' to display the registerform but when I do that I get the 'methodnotallowedhttpexception error.
Currently when I enter data into the form and press submit it just refreshes the page. Any help would greatly be appreciated, thanks
web.php
Route::post('registerForm', 'AuthController#viewregisterForm');
Route::post('registerUser', 'AuthController#registerUser');
AuthController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//use Illuminate\Support\Facades\Auth;
//use App\Http\Requests;
use App\User;
class AuthController extends Controller
{
function viewregisterForm()
{
return view('register/registerForm');
}
function registerUser(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'password' => 'required',
'dateofbirth' =>'required',
]);
//create a Film object
$user = new $User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = $request->password;
$user->dateofbirth = $request->dateofbirth;
$user->role = 1;
$user->save();
return redirect('all');
}
}
?>
registerForm.blade
#extends('layouts.master')
#section('title', 'Register user')
#section('content')
<form action="{{url('registerForm')}}" method="POST">
{{ csrf_field() }}
<h1>Register user</h1>
<div>
<label for="title">Enter name</label>
<input type="text" name="name" id="name">
</div>
<div>
`enter code here`<label for="title">Enter email</label>
<input type="text" name="email" id="email">
</div>
<div>
<label for="title">Enter password</label>
<input type="text" name="password" id="password">
<label for="title">Enter date of birth</label>
<input type="text" name="dateofbirth" id="dateofbirth">
</div>
<input type="submit" name="submitBtn" value="Add User">
</form>
#endsection
You need to change the action of your form submit
from
<form action="{{ url('registerForm') }}" method="POST">
to
<form action="{{ route('register-user') }}" method="POST">
and change the route from
Route::post('registerUser', 'AuthController#registerUser');
to
Route::post('registerUser', ['as' => 'register-user', 'uses' => 'AuthController#registerUser']);
Try to change your routes to this:
Route::get('registerForm', 'AuthController#viewregisterForm');
Route::post('registerForm', 'AuthController#registerUser');

MethodNotAllowedHttpException?

I'm getting this error when i want to delete ?
<form action="{{ URL::route('admin.property.features.delete',$feature-
>id) }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button class="btn-block btn-link delete-btn admin-delete">Delete</button>
</form>
this is my route:
Route::get('admin/property/features/{id}/delete', ['as' => 'admin.property.features.delete', 'uses' => 'Admin\AdminPropertyFeaturesController#destroy']);
controller:
public function destroy($feature_id){
$feature = Feature::findOrFail($feature_id);
dd($feature);
$feature->delete();
return redirect()->back()->withFlashMessage('Property features has been deleted successfully!!');
}
Simple error. You are only accepting GET request in your route list. And the request you are making from view is POST. So that's why it showing method is not allowed.
Change the route to POST and it will work.
Route::post('admin/property/features/{id}/delete', ['as' => 'admin.property.features.delete', 'uses' => 'Admin\AdminPropertyFeaturesController#destroy']);
Edit:
Use any to accept any kind of the requests.
Route::any('admin/property/features/{id}/delete', ['as' => 'admin.property.features.delete', 'uses' => 'Admin\AdminPropertyFeaturesController#destroy']);

Submitting form input after logging in with laravel

I have the view:
<form class="text-center" action="{{route('PostComment')}}" method="POST">
<div class="form-group">
<textarea class="form-control" name="Comment" id="exampleTextarea" placeholder="Write down your thought here..." rows="4"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
And also the route:
Route::post('/Comment', [
'uses' => 'CommentController#Comment',
'as' => 'PostComment',
'middleware' => 'auth'
]);
And the controller(not so important):
public function Comment(Request $request)
{
$this->validate($request, [
'Comment' => 'required|min:10|max:100',
]); // validation of comment
$NewComment = new Comment();
$NewComment->user_id = Auth::user()->id;
$NewComment->text = $request['Comment'];
$NewComment->save();
return redirect()->route('Debate');
}
My question is that when you submit the form data and you are not logged in, you have to log in but the form isn't submitted?(edited:using laravel auth)
How to make this thing work, you fill the textarea, you forgot to log in, you log in and the form is submited?

Laravel 5 how to use get parameter from url with controller's sign in method

I'm trying to implement a login system where the user will be redirect back only if there is a get parameter in the url, else it will be redirect to the profile page.
So, if the uri is something like this (no get parameter)
/login
if success, the user will be redirect to the profile page.
But if the uri has the get parameter like for example
/login?r=articles
the user will be redirected to the articles page instead of the default route to the profile page.
Question is, in the controller, how can do this, if possible, or how can I check for the get parameter?
routes.php
// Signin
Route::post('/account/signin', [
'uses' => 'UserController#postSignIn',
'as' => 'user.signin',
]);
UserController.php
// Signin
public function postSignIn(Request $request)
{
$this->validate($request, [
'login-email' => 'required|email',
'login-password' => 'required'
]);
if ( Auth::attempt(['email' => $request['login-email'], 'password' => $request['login-password']]) )
{
// Tried this, isn't working... (maybe something's missing ??)
$redirect = $request->input('r');
if ($redirect) {
return redirect()->route($redirect);
}
// -->
return redirect()->route('user.account');
}
return redirect()->back();
}
signin.blade.php
<form role="form" action="{{ route('user.signin') }}" method="post" class="login-form" name="login">
<div class="form-group {{ $errors->has('login-email') ? 'has-error' : '' }}">
<label class="sr-only" for="email">Email</label>
<input type="text" name="login-email" value="{{ Request::old('login-email') }}" placeholder="Email..." class="form-username form-control" id="form-username">
</div>
<div class="form-group {{ $errors->has('login-password') ? 'has-error' : '' }}">
<label class="sr-only" for="password">Password</label>
<input type="password" name="login-password" value="{{ Request::old('login-password') }}" placeholder="Password..." class="form-password form-control" id="form-password">
</div>
<div class="form-group">
<input type="checkbox" name="remember" value="{{ Request::old('remember') }}" id="remember">
Remember
</div>
<button type="submit" class="btn">Sign in!</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
Thanks.
Updated
Thank you all for your replies, the fact is that I'm still getting to know Laravel and that's probably why I can't implement it right the solutions that you guys shared.
So this said, I got it working by creating a conditional hidden field that holds the query value and this way once the user submits the form, it will be passed with the rest of the $response arguments.
signin.blade.php
<form role="form" action="{{ route('user.signin') }}" method="post" class="login-form" name="login">
<div class="form-group {{ $errors->has('login-email') ? 'has-error' : '' }}">
<label class="sr-only" for="email">Email</label>
<input type="text" name="login-email" value="{{ Request::old('login-email') }}" placeholder="Email..." class="form-username form-control" id="form-username">
</div>
<div class="form-group {{ $errors->has('login-password') ? 'has-error' : '' }}">
<label class="sr-only" for="password">Password</label>
<input type="password" name="login-password" value="{{ Request::old('login-password') }}" placeholder="Password..." class="form-password form-control" id="form-password">
</div>
<div class="form-group">
<input type="checkbox" name="remember" value="{{ Request::old('remember') }}" id="remember">
Remember
</div>
<button type="submit" class="btn">Sign in!</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
<!-- Verify condition -->
#if(isset($_GET['referer']))
<input type="hidden" name="referer" value="{{ $_GET['referer'] }}">
#endif
</form>
UserController.php
// Signin
public function postSignIn(Request $request)
{
$this->validate($request, [
'login-email' => 'required|email',
'login-password' => 'required'
]);
if ( Auth::attempt(['email' => $request['login-email'], 'password' => $request['login-password']]) )
{
// Check for the new argument 'referer'
if (isset($request->referer)) {
return redirect()->route($request->referer);
}
// -->
return redirect()->route('user.account');
}
return redirect()->back();
}
Like so, it works.
Don't know if it's a viable and secure way to do it in Laravel 5, but it is working.
When you have an URI such as login?r=articles, you can retrieve articles like this:
request()->r
You can also use request()->has('r') to determine if it's present in the URI.
And request()->filled('r') to find out if it's present in the URI and its value is not empty.
// only query
$query_array = $request->query();
or
$query = $request->query('r');
// Without Query String...
$url = $request->url();
// With Query String...
$url = $request->fullUrl();
If you are using Laravel then use their helper which works just out of the box, i.e. if your route or url has a auth middlewere and user is not logged in then it goes to login and in your postSign or inside attempt just
return redirect()->intended('home'); //home is the fallback if no intended url is provide
UserController
public function postSignIn(Request $request){
$this->validate($request, [
'login-email' => 'required|email',
'login-password' => 'required'
]);
if ( Auth::attempt(['email' => $request['login-email'], 'password' => $request['login-password']]) )
{
return redirect()->intended('user.account);
}
return redirect()->back();
}
first use this
use Illuminate\Support\Facades\Input;
then you can get any data from your url
$name=Input::get('term', false); //term is the parameter name we get from URL
If you are still in this quest you can use
Request
like
$request->r

Resources