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();
}
Related
In my Laravel-5.8, I am using the index as the search page:
public function index()
{
$search = '';
if (request('search'))
{
$search = request('search');
return redirect()->route('client', [$search]);
}
return view('index', ['search' => $search]);
}
public function client($clientid)
{
$myparam = $clientid;
$client = new Client();
$res = $client->request('GET','https://example/{$myparam}', [
'query' => ['key' => 'jkkffd091']
])->getBody();
$geoLocation = json_decode($res->getContents(), true);
return view('client', [
'geoLocation' => $geoLocation
]);
}
Here is my index blade:
<form method="GET" action="{{ route('index', ['search' => $search]) }}">
<div class="field">
<label class="label blog-sidebar-label" for="search">
<input class="search-field" type="search" name="search" id="search" placeholder="client id..." value="{{ $search }}">
</label>
</div>
</form>
I have this route:
Route::get('/', [HomeController::class, 'index'])->name('index');
Route::get('client/{search}', [HomeController::class, 'client'])->name('client');
What I want to achieve is that when the search button for index is submitted with the clientid in the text input field, it should take the clientid as parameter and pass it to client controller. Also redirect to client blade.Then when nothing is in the text input field, it should indicate.
But it is not redirecting. How do I achieve this?
Thanks
Probably you want to submit without a submit button. Then you can submit with JS :
<form id="submitForm" method="GET" action="{{ route('index', ['search' => $search]) }}">
<div class="field">
<label class="label blog-sidebar-label" for="search">
<input class="search-field" type="search" name="search" id="search" placeholder="client id..." value="{{ $search }}">
</label>
</div>
</form>
<!-- JS code below -->
<script>
document.getElementById("search").onchange = function() {myFunction()};
function myFunction() {
document.getElementById("submitForm").submit();
}
</script>
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',
I'm receiving the error "MethodNotAllowedHttpException
No message" on submit of my user's form, which is meant to update the user's table. I have two post forms on the same page and two post routes, would that have something to do with it?
I will include all the routes and another form that might be conflicting with it.
web.php
Route::get('profile','userController#profile');
Route::post('profile', 'userController#update_avatar');
Route::post('profile-update', 'userController#update_account'); //this ones not working
userController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
use Image;
class UserController extends Controller
{
//
public function profile()
{
return view('profile', array('user' => Auth::user()) );
}
public function update_avatar(Request $request)
{
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save( public_path('/uploads/avatars/' . $filename) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('profile', array('user' => Auth::user()) );
}
public function update_account(Request $request, $id) //the function with the error
{
User::update([
'id' => Auth::user()->id,
'name' => $request->name,
'email' => $request->email
]);
return redirect('/profile');
}
}
profile.blade.php
<img src="/uploads/avatars/{{ $user->avatar }}" style="width:150px;height:150px;float:left;border-radius:50%;margin-right:25px">
<h2>{{ $user->name }}'s Profile</h2>
<form enctype="multipart/form-data" action="/profile" method="post">
<label>Update Profile Image</label>
<input type="file" name="avatar">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class=" btn btn-sm btn-light" style="color:#2b2b2b;" value="Update Image">
</form>
<form method="post" action="/profile-update"> <!-- The form with the error -->
{{ method_field('put') }}
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT" />
<label>Username</label>
<input type="text" name="name" class="form-control" value="{{ $user->name }}">
<label>Email</label>
<input type="email" name="email" class="form-control" value="{{ $user->email }}">
<input type="submit" id="update-account" class="btn btn-success" value="Update">
</form>
try this method:
public function update_account(Request $request, $id)
{
$user = User::find($id)
$user->name = $request->name;
$user->email = $request->email;
$user->update();
return redirect('/profile');
}
You don't have any route which can handle the PUT request for "profile-update". In your form you have defined the following function.
{{ method_field('put') }}
This helper function generate an hidden input field which will be used by Laravel to process the current request only as PUT.
To make this work, you either have to make your make your request POST by removing the above helper function or change your route method to PUT.
Route::put('profile-update', 'userController#update_account');
For those that might need the same answer, to fix this I had to play about with it for quite some time and used bits from the suggested answers to solve the issue completely.
I changed the route method to put in web.php.
Replaced my update_account function with #TonzFale answer but replaced $user = User::find($id)with $user = User::find(Auth::user()->id);.
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' ]);
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');