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

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');

Related

How to get session mobile in laravel

I want to after registered a user name, age, mobile and ... get the session of mobile because in next page, I want to get mobile in input hidden.
Attention: I did not do session at all.
I think it's like this:
RegisterController.php
public function register(Request $request, User $user)
{
$code = rand(10000,99999);
session->get('mobile')
$user = \App\User::create([
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'gender' => $request->gender,
'mobile' => $request->mobile,
'code' => $code,
.
.
.
return redirect()->route('code')->with('mobile', $request->mobile);
}
It redirect to this page.
Code
code.blade.php
<form action="{{ route('send') }}" method="post">
{{ csrf_field() }}
<input type="hidden" class="form-control" value="{{ session->mobile }}" name="mobile" id="mobile">
<div class="form-group">
<label for="code">کد</label>
<input type="text" class="form-control" name="code" id="code">
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger" id="btn-ok">OK</button>
</div>
</form>
use Illuminate\Support\Facades\Session;
For saving in session use
Session::put('mobile', $request->mobile);
then retrieve it using
Session::get('mobile');

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();
}

ReflectionException in Route.php line 280: Method App\Http\Controllers\UserController::Signup() does not exist

I am a new programmer on laravel. I am currently using laravel 5.2. I ran into this error while i was trying to input data into a form i created as welcome.blade.php. I have check the route and it seems very okay. I dont what i may be doing wrong. This is problem display
ReflectionException in Route.php line 280:
Method App\Http\Controllers\UserController::Signup() does not exist
UserController
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
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()->back();
}
public function postSignIn(Request $request)
{
}
}
Routes
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
});
Route::post('/signup', [
'uses' => 'UserController#Signup',
'as' => 'signup',
]);
});
welcome.blade.php
#extends('layouts.master')
#section('title')
Welcome!
#endsection
#section('content')
<div class="row">
<div class="col-md-6">
<h2>Sign Up</h2>
<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">Your First Name</label>
<input class="form-control" type="text" name="first_name" id="first_name">
</div>
<div class="form-group">
<label for="password">Your Password</label>
<input class="form-control" type="text" name="password" id="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{Session::token()}}">
</form>
</div>
<div class="col-md-6">
<h2>Sign In</h2>
<form action="#" 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">Your Password</label>
<input class="form-control" type="text" name="password" id="password"></div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
#endsection
You're using diffrent function name according to route, both will be same.
Replace your function name with this:-
postSignUp to SignUp
Thanks for your contribution as suggested i change the postSignUp to SignUp in the userController
public function postSignUp(Request $request)
change to
public function SignUp(Request $request)
also in the route I change the
'uses' => 'UserController#Signup',
change to
'uses' => 'UserController#SignUp',

Laravel post request not works

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' ]);

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