419 Page Expired in laravel 7 - php

I have error 419 | Page Expired when i login in my website, i alrady use #csrf in my form.
what can i do to fix this problem ?
this is my login.php file
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Login') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('login') }}">
#csrf
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="current-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>

try clearing the browser cache as well as laravel with php artisan cache:clear.
Sometime 419 is due to stale cache.

Related

Only some values are editing with a form Laravel 8

I am using a form and I don't know why I can edit with it the values except in the fields apellido and cedula.
I am using the same logic in all the form fields so I dunno what can be causing it.
I'm gonna post the code of the view, controller, and model.
View
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Editar Médico</h1>
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('medico.update', $medico) }}">
#csrf
#method('PUT')
<div class="form-group row">
<label for="nombre" class="col-md-4 col-form-label text-md-right">{{ __('Nombre') }}</label>
<div class="col-md-6">
<input id="nombre" type="text" class="form-control #error('nombre') is-invalid #enderror" name="nombre" value="{{ $medico->nombre}}" required autocomplete="nombre" autofocus>
#error('nombre')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="apellido" class="col-md-4 col-form-label text-md-right">{{ __('Apellido') }}</label>
<div class="col-md-6">
<input id="apellido" type="text" class="form-control #error('apellido') is-invalid #enderror" name="apellido" value="{{ $medico->apellido}}" required autocomplete="apellido" autofocus>
#error('apellido')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="cedula" class="col-md-4 col-form-label text-md-right">{{ __('Cédula') }}</label>
<div class="col-md-6">
<input id="cedula" type="text" class="form-control #error('cedula') is-invalid #enderror" name="apellido" value="{{ $medico->cedula}}" required autocomplete="cedula" autofocus>
#error('cedula')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('Email') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ $medico->email}}" required autocomplete="email" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="telefono" class="col-md-4 col-form-label text-md-right">{{ __('Teléfono') }}</label>
<div class="col-md-6">
<input id="telefono" type="text" class="form-control #error('telefono') is-invalid #enderror" name="telefono" value="{{ $medico->telefono}}" required autocomplete="telefono" autofocus>
#error('telefono')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="direccion" class="col-md-4 col-form-label text-md-right">{{ __('Dirección') }}</label>
<div class="col-md-6">
<input id="direccion" type="text" class="form-control #error('direccion') is-invalid #enderror" name="apellido" value="{{ $medico->direccion}}" required autocomplete="direccion" autofocus>
#error('direccion')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="ciudadResi" class="col-md-4 col-form-label text-md-right">{{ __('Ciudad de Residencia') }}</label>
<div class="col-md-6">
<input id="ciudadResi" type="text" class="form-control #error('ciudadResi') is-invalid #enderror" name="apellido" value="{{ $medico->ciudadResi}}" required autocomplete="ciudadResi" autofocus>
#error('ciudadResi')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="fechaNacimiento" class="col-md-4 col-form-label text-md-right">{{ __('Fecha de Nacimiento') }}</label>
<div class="col-md-6">
<input id="fechaNacimiento" type="date" class="form-control #error('fechaNacimiento') is-invalid #enderror" name="fechaNacimiento" value="{{ $medico->fechaNacimiento}}" required autocomplete="fechaNacimiento" autofocus>
#error('fechaNacimiento')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="genero" class="col-md-4 col-form-label text-md-right">{{ __('Genero') }}</label>
<div class="col-md-6">
<input id="genero" type="text" class="form-control #error('genero') is-invalid #enderror" name="genero" value="{{ $medico->genero}}" required autocomplete="genero" autofocus>
#error('genero')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Editar') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Controller
public function updateMedico(Request $request, $id) {
$medico = Persona::findOrFail($id);
$medico->fill($request->all());
if($medico ->save()) {
return redirect()->route('personaMostrarMedicos');
} else {
return redirect()->route('medico.edit');
}
}
Model
public $timestamps =false;
protected $fillable = [
'nombre',
'apellido',
'cedula',
'email',
'telefono',
'direccion',
'ciudadResi',
'fechaNacimiento',
'genero',
'estado',
'idTipoPersona'
];
I have no idea what could be wrong because only those 2 fields I mention are the ones I can't update, the other ones are fine.
In case it can helps this is also my database model
After using dd($request->all()); in the controller this is the output
What I have in the form before clicking the update button and the output of dd($request->all())
I am not sure if this is the problem, but you have 2 name="apellido", check cedula, it has that name, so that is wrong.
Remember that when you send a form, the way to get the value is going to use the name property and not the id as that is pure CSS.
Change this:
<input id="cedula" type="text" class="form-control #error('cedula') is-invalid #enderror" name="apellido" value="{{ $medico->cedula}}" required autocomplete="cedula" autofocus>
To this:
<input id="cedula" type="text" class="form-control #error('cedula') is-invalid #enderror" name="cedula" value="{{ $medico->cedula }}" required autocomplete="cedula" autofocus>
You have two inputs with the same name apellido this is why it isn't working properly.
Change this -
<input id="cedula" type="text" class="form-control #error('cedula') is-invalid #enderror" name="apellido" value="{{ $medico->cedula}}" required autocomplete="cedula" autofocus>
To this -
<input id="cedula" type="text" class="form-control #error('cedula') is-invalid #enderror" name="cedula" value="{{ $medico->cedula}}" required autocomplete="cedula" autofocus>
It should be working then.

Laravel displays error 419 page expired on register

Whenever I try to register it says 419 page expired. I have try cleaning cache and even checked other answers such as adding #csrf in the form but nothing worked. It says the same for login as well.
My code: (register.blade.php)
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="new-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
My code: (login.blade.php)
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Login') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('login') }}">
#csrf
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="current-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<div class="col-md-6 offset-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
<label class="form-check-label" for="remember">
{{ __('Remember Me') }}
</label>
</div>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>
#if (Route::has('password.request'))
<a class="btn btn-link" href="{{ route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
#endif
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
I do have the #csrf after form starts (as suggested in other answers) but this didn't work either. How can I fix this error? If you need any more details or code please let me know I will upload it.

Removing the laravel error messages once the text field is clicked

Currently I'm showing my form validation error messages in the front end blade under the relevant field.
But the messages are there until the user resubmit the form, they wont removed.
I want to hide each error message once the user clicks on the field (or in the most user friendly way).
This is form code for my register blade,
<form method="POST" action="{{ route('register') }}">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('sentence.First Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ old('name') }}" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="last_name" class="col-md-4 col-form-label text-md-right">{{ __('sentence.Last Name') }}</label>
<div class="col-md-6">
<input id="last_name" type="text" class="form-control #error('name') is-invalid #enderror" name="last_name" value="{{ old('name') }}" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('sentence.E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="text" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="moblie" class="col-md-4 col-form-label text-md-right">{{ __('Mobile') }}</label>
<div class="col-md-6">
<input id="mobile_1" type="tel" class="form-control #error('mobile') is-invalid #enderror" name="mobile" value="{{ old('mobile') }}" style="min-width: 330px;" autofocus>
#error('mobile')
<br/>
<span class="help-block" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="username" class="col-md-4 col-form-label text-md-right">{{ __('sentence.Username') }}</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control #error('username') is-invalid #enderror" name="username" value="{{ old('username') }}" autofocus>
#error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('sentence.Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" >
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('sentence.Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" >
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('sentence.Register') }}
</button>
</div>
</div>
</form>
I think the most user-friendly way is to have client-side validation,
Security: Server-side validation
User friendly: Client-side validation
For client-side, you can use jQuery, In your case:
<script>
jQuery( document ).ready(function() {
// event for click on input (also you can use click)
//better to change form to .yourFormClass
$('form input[type=text]').focus(function(){
// get selected input error container
$(this).siblings(".invalid-feedback").hide();
});
});
</script>
Add this code before closing body tag,(Don't forget to use the jQuery library ),
Hope it helps

Laravel - login messages

I'm currently working on Laravel 5.8 version and I'm using Laravel make:auth option. That created me login and registration and all I did i add a phone number. So this is how it is looking in my register.blade.php :
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="new-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-md-4 col-form-label text-md-right">{{ __('Phone Number') }}</label>
<div class="col-md-6">
<input id="phone" type="phone" class="form-control" name="phone" required>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
So this section:
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
shows me span message if email has already been taken. So how can I do that #error('phone') for my phone? I coded in my RegisterController.php that my phone my be unique:users?
Please help me with this #error message!
This will check User Phone Number Like Email. It Will Allocate Unique user Phone Number
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'phone' => 'required|unique:users,phone'
]);
}
For Displaying Errors In Register Blade
#error('phone')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
You need to add something like this to RegisterController.php
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'phone' => 'required'
]);
}

SQLSTATE[HY000]: General error: 1364 Field 'username' doesn't have a default value

hello I try to create register in laravel 5.8 but when i try to register have this error :
tableuser.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('username')->unique();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('admin')->default(false);
$table->rememberToken();
$table->timestamps();
});
}
registe.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
#csrf
<div class="form-group row">
<label for="username" class="col-md-4 col-form-label text-md-right">{{ __('Username') }}</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control #error('username') is-invalid #enderror" name="username" value="{{ old('username') }}" required autocomplete="username" autofocus>
#error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="new-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
SQLSTATE[HY000]: General error: 1364 Field 'username' doesn't have a
default value (SQL: insert into users (name, email, password,
updated_at, created_at) values (denden73, denden73#gmail.com,
$2y$10$x.J7316.UH.UFnzZaeI5F.TTXkYyd.xVUlrjV6EoHA7J88R0X9Ode,
2019-07-25 12:50:54, 2019-07-25 12:50:54))
Try to add this to your User.php model file
protected fillable = ['username', 'email', 'password'];
Make sure you've followed this rule or not.
1) use nullable() while migrate.
-> It becuase of when you are inserting any record and if the field value is not set then this error would occur.
$table->string('username')->nullable();
2) Fillable in your model.
-> IF you didn't set nullable as a column, Now while inserting record you tried to insert value but if that value is not fillable in the model then it'll not insert so it this error would occur.
protected $fillable = [
'username', 'name', 'email','password','admin'
];
3) mention the username while creating a new user.
-> Sometime by mistake you're not passing that value while creating so if that field is not null in MySQL then this erro would occur.
$user = new App\User;
$user->username = 'xyz';
$user->save();
Your insert query like:
DB::table('users')->insert(array('username'=>$request->username,'name'=>$request->name,'email'=>$request->email,'password'=>$request->password));

Categories