Adding new fields into Laravel 5.8 defaut user table - laravel

I have updated the laravel users table by adding few more field.usertype was one of it. Then I have updated the User model fillable field and also I have added required input types into my form. After all of these I have run php artisan migrate:refresh after that I can see that my table field also updated. But when I submit the form it generates the below error saying usertype dosent have a default value. usertype field is not a null filed .Can anyone say me whats wrong in this. For more references please refer below codes.
migration
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
//added by myself
$table->string('usertype');
$table->boolean('activestatus')->default(false);
//
$table->rememberToken();
$table->timestamps();
});
User Model
protected $fillable = [
'name',
'email',
'password',
'usertype',
];
User Registration form
#csrf
<input type="text" name="usertype" id="hiddenUserType" value="a" hidden/>
<div class="form-group{{ $errors->has('name') ? ' has-danger' : '' }}">
<div class="input-group input-group-alternative mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-hat-3"></i></span>
</div>
<input class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" placeholder="{{ __('Name') }}" type="text" name="name" value="{{ old('name') }}" required autofocus>
</div>
#if ($errors->has('name'))
<span class="invalid-feedback" style="display: block;" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
<div class="form-group{{ $errors->has('email') ? ' has-danger' : '' }}">
<div class="input-group input-group-alternative mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-email-83"></i></span>
</div>
<input class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" placeholder="{{ __('Email') }}" type="email" name="email" value="{{ old('email') }}" required>
</div>
#if ($errors->has('email'))
<span class="invalid-feedback" style="display: block;" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
<div class="form-group{{ $errors->has('password') ? ' has-danger' : '' }}">
<div class="input-group input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-lock-circle-open"></i></span>
</div>
<input class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" placeholder="{{ __('Password') }}" type="password" name="password" required>
</div>
#if ($errors->has('password'))
<span class="invalid-feedback" style="display: block;" role="alert">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
<div class="form-group">
<div class="input-group input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-lock-circle-open"></i></span>
</div>
<input class="form-control" placeholder="{{ __('Confirm Password') }}" type="password" name="password_confirmation" required>
</div>
</div>
{{-- <div class="text-muted font-italic">
<small>{{ __('password strength') }}: <span class="text-success font-weight-700">{{ __('strong') }}strong</span></small>
</div> --}}
<div class="row my-4">
<div class="col-12">
<div class="custom-control custom-control-alternative custom-checkbox">
<input class="custom-control-input" id="customCheckRegister" type="checkbox">
<label class="custom-control-label" for="customCheckRegister">
<span class="text-muted">{{ __('I agree with the') }} {{ __('Privacy Policy') }}</span>
</label>
</div>
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary mt-4">{{ __('Create account') }}</button>
</div>
</form>
Usercontroller
public function store(UserRequest $request, User $model)
{
$model->create($request->merge([
'password' => Hash::make($request->get('password'))
])->all());
return redirect()->route('user.index')
->withStatus(__('User successfully created.'));
}
This is the error it generates while I try to submit the form
SQLSTATE[HY000]: General error: 1364 Field 'usertype' doesn't have a
default value (SQL: insert into users (name, email, password,
updated_at, created_at) values (Nipun Tharuksha, nipun#gmail.com,
$2y$10$fHtc0yTpAFrGcO7dfg1qRe.EYZY/UNY4Ygn5qvUvcCSwoHjUjB6Gu,
2019-04-19 15:36:36, 2019-04-19 15:36:36))
DB

As the error says you are not providing any value for the usertype column, and you are trying to save it with null value. So in that case you can change the migration to allow null by default like this:
$table->string('usertype')->nullable();
And once you change the migration you will have to re-run it using:
php artisan migrate:fresh
Warning: Keep in mind that this will destroy your current data.

Thanks for commenting on this. I was updating Controller UserController . But as #tharakadilshan mentioned I updated mt RegisterController and now its working. Once again thanks for all the comments.
Thanks

Related

How do I create date in register form

How do I create date of birth or date in register form? I am trying to make Register form. How can I do this?
Output error
In user models:
public function setDateOfBirthAttribute($value)
{
$this->attributes['date_of_birth'] = $value ? Carbon::createFromFormat(config('project.date_format'), $value)->format('Y-m-d') : null;
}
In Blade files:
<div class="form-group bmd-form-group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="material-icons">event</i>
</span>
</div>
<input name="date_of_birth" type="date" class="form-control" placeholder="{{ trans ('global.date_of_birth') }}..." value="{{ old('date_of_birth' }} " required autocomplete="name" autofocus> config('project.date_format')
</div>
#error('date_of_birth')
<div class="error" for="date_of_birth">
{{ $message }}
</div>
#enderror
</div>

Laravel Auth not working after moving to remove

I have moved my local laravel Application to Production. On my local pc the register function work's well (using Laravel Auth). On the remote host nothing happeds when I submit the register form. No validation error or something else. What is wrong with my application?
I got an 404 while the process. Here my register routes:
Route::get('register', 'Auth\RegisterController#showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController#register');
Here the content of my register.blade.php file:
<form id="js-validation-signup" action="{{ route('register') }}" method="post">
#csrf
<div class="py-3">
<div class="form-group">
<input type="text"
class="form-control form-control-lg form-control-alt{{ $errors->has('first_name') ? ' is-invalid' : '' }}"
id="first_name" name="first_name" placeholder="{{ __('First Name') }}"
value="{{ old('first_name') }}" autocomplete="off">
#if ($errors->has('first_name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('first_name') }}</strong>
</span>
#endif
</div>
<div class="form-group">
<input type="text"
class="form-control form-control-lg form-control-alt{{ $errors->has('name') ? ' is-invalid' : '' }}"
id="name" name="name" placeholder="{{ __('Name') }}"
value="{{ old('name') }}" autocomplete="off">
#if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
<div class="form-group">
<input type="email"
class="form-control form-control-lg form-control-alt{{ $errors->has('email') ? ' is-invalid' : '' }}"
id="email" name="email" placeholder="{{ __('E-Mail Address') }}"
autocomplete="off" value="{{ old('email') }}">
#if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
<div class="form-group">
<input type="password"
class="form-control form-control-lg form-control-alt{{ $errors->has('password') ? ' is-invalid' : '' }}"
id="password" name="password" placeholder="{{ __('Password') }}">
#if ($errors->has('password'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
<div class="form-group">
<input type="password" class="form-control form-control-lg form-control-alt"
id="password_confirmation" name="password_confirmation"
placeholder="{{ __('Confirm Password') }}">
</div>
<div class="form-group">
<div class="input-group">
<input class="form-control form-control-lg form-control-alt{{ $errors->has('application_name') ? ' is-invalid' : '' }}"
id="application_name"
name="application_name" type="text"
placeholder="{{ __('Application Name') }}" autocomplete="off"
value="{{ old('application_name') }}">
<div class="input-group-append">
<span class="input-group-text input-group-text-alt">.example.com</span>
</div>
#if ($errors->has('application_name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('application_name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="custom-control custom-checkbox custom-control-primary">
<input type="checkbox" class="custom-control-input" id="signup-terms"
name="signup-terms">
<label class="custom-control-label" for="signup-terms">I agree to Terms &
Conditions</label>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-block btn-hero-lg btn-hero-primary">
<i class="fa fa-fw fa-user-plus mr-1"></i> Sign Up
</button>
<p class="mt-3 mb-0 d-lg-flex justify-content-lg-between">
<a class="btn btn-sm btn-light d-block d-lg-inline-block mb-1" href="#">
<i class="fa fa-book text-muted mr-1"></i> Read Terms
</a>
</p>
</div>
</form>
I think i had the same problem with login here.
my problem was that I used Laravel's auth login and inside Auth/LoginController has a
use AuthenticatesUsers which uses a trait that is inside vendor and Auth folder and when I wanted to use git clone to get the project on another computer and used composer install to install the used packages for the project my vendor changed to default and all the login code which was in AuthenticatesUsers trait removed and changed to default
The route() function is for named routes.
It seems that you named your get route but didn't do that with the corresponding post route. Accessing route('register') with a POST method will result in a 404 error.
Try to name the post route as well or use action('Auth\RegisterController#register')
<form id="js-validation-signup" action="{{action('Auth\RegisterController#register')}}" method="post">

Catch ONLY the invalid credentials error in a login.blade Laravel

I am using the custom login controller from Laravel. I have the validation messages in a below the input fields.
Everything works fine.
What I want now is to show a message “incorrect credentials” ONLY when user or password are incorrect and in a different div. I mean, if other validation error triggers, this message should not be visible.
The errors->has(‘email’) array catches this error but also the rest, for instance, ‘the field is required’.
Does anybody know how to write a condition that only catches this ‘invalid credentials’ error message?
Below the template.
Thanks in advance for your help!
#extends ('layouts.default')
#section('content')
#if ($errors->has('email')) {{-- I want the credential error here, but only
for credential error is triggered --}}
<div class="warning">
<div class="input-icon">
<i style="font-size:1.5em; color:Tomato; margin-right:5px;" class="fas
fa-exclamation-triangle"></i>
</div>
<p>Usuario o contraseña incorrecta</p>
</div>
#endif
<main class="login-page">
<div class="contact login">
<div class="titulos">
<p>Ingresar</p>
<p>Soy nuevo</p>
</div>
<form method="post">
#csrf
<div class="input-group input-group-icon">
<input type="email" name="email" placeholder="Correo electrónico"
value="{{ old('email') }}" autofocus/>
<div class="input-icon">
<i class="fas fa-envelope"></i>
</div>
<span class="obligatorio" > {{ $errors->first('email') }}</span>
</div>
<div class="input-group input-group-icon">
<input type="password" name="contraseña"
placeholder="Contraseña"/>
<div class="input-icon">
<i class="fas fa-lock"></i>
</div>
<span class="obligatorio" >{{ $errors->first('contraseña') }}
<div class="input-group">
<input type="submit" value="Ingresar" />
<a href="{{ route('password.request') }}">Olvidé mi
contraseña</a>
</div>
<div>
<label>
<input type="checkbox" name="recordar" id="cbox1"
value="recordar" {{ old('recordar') ? 'checked' : '' }}>
<span>Recordar mi usuario</span>
</label>
</div>
</form>
</div>
</main>
#endsection
In an old beginner Project of mine I did it this way:
<div class="form-group{{ $errors->has('text') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">#lang('views/auth/login.username')</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name">
#if ($errors->has('text'))
<span class="help-block">
<strong>{{ $errors->first('text') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">#lang('views/auth/login.password')</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password">
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
And inside the Controller I just catched the Exceptions:
public function authenticate(Request $request)
{
try {
$this->ldapHelper->checkCredentials($request->name, $request->password);
$ldapUserData = $this->ldapHelper->getFormattedUserData(request('name'));
$this->sessionService->login($ldapUserData);
return redirect()->route('form.formular');
} catch (\Exception $e) {
return back()->withInput()->withErrors([
'message' => $e->getMessage(),
]);
}
}
Maybe it helps!

Missing required parameters for Route with custom password reset

I'm trying to add new type of users "Client" into existing project.
It already had "Admin" user, which does not have email password reset function.
I duplicated and modified native Laravel out of the box Auth controllers, blade forms and model and now I can login and logout as Client (guard:client).
My problem is in password reset function.
"Forgot Your Password?" button redirects to the right url (http://127.0.0.1:8000/client/password/reset).
"Send Password Reset Link" button generates email with link (http://127.0.0.1:8000/client/password/reset/a7558b0f294af7cdaeafd73617f664e1d76ed27d567d648e8b468eb9edcc9c2d)
But when push that link I get following error:
Missing required parameters for [Route: client.password.reset] [URI:
client/password/reset/{token}]. (View:
C:\wamp64\www\testkz\resources\views\index\client\reset.blade.php)
Where reset.blade.php is a copy of resources\views\auth\passwords\reset.blade.php
My routes
Route::post('/password/email','Client\ClientForgotPasswordController#sendResetLinkEmail')->name('client.password.email');
Route::get('/password/reset','Client\ClientForgotPasswordController#showLinkRequestForm')->name('client.password.request');
Route::post('/password/reset','Client\ClientResetPasswordController#reset');
Route::get('/password/reset/{token}','Client\ClientResetPasswordController#showResetForm')->name('client.password.reset');
ClientResetPasswordController
<?php
namespace App\Http\Controllers\Client;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Password;
use Auth;
class ClientResetPasswordController extends Controller
{
use ResetsPasswords;
protected $redirectTo = '/client';
public function __construct()
{
$this->middleware('guest:client');
}
protected function guard()
{
return Auth::guard('client');
}
protected function broker()
{
return Password::broker('clients');
}
public function showResetForm(Request $request, $token = null)
{
return view('index.client.reset')->with(
['token' => $token, 'email' => $request->email]
);
}
}
blade
#extends('index.layout.layout')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Reset Client Password</div>
<div class="panel-body">
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ route('client.password.reset') }}">
{{ csrf_field() }}
<input type="hidden" name="token" value="{{ $token }}">
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ $email or old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
#if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Reset Password
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Make sure form method id POST
<form class="form-horizontal" method="POST" action="{{ route('password.request') }}">
{{ csrf_field() }}
<input type="hidden" name="token" value="{{ $token }}">
</form>
I upgraded my project from Laravel 5.3 to 5.5, but used old version of reset.blade.php

Laravel form displayed as raw text

My problem is probably related to server configuration problem. This is the issue. I am developing a Laravel website that works fine when developing on my local machine (using the laradock docker environment).
I wanted to run it on a small local machine so I turned on a fresh Ubuntu 16.04 server and installed docker on it. Checked out my code and ran composer install.
After going to the main registration page, I see that parts of the registration form are displayed as raw text :
I don't see any errors when using the developers page and also no errors in the php-fpm, workspace or nginx container from laradock that hosts the website.
Any idea?
code:
<?php
$ip = get_ip_address();
$countries = [
"Afghanistan" => "Afghanistan",
"Albania" => "Albania",
"Zimbabwe" => "Zimbabwe"
];
?>
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Register</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group">
<label for="country" class="col-md-4 control-label">Select your country</label>
<div class="col-md-6">
{{Form::select("country", $countries, null, ['type' => 'text','class' => 'form-control','placeholder' => 'Pick your country...'])}}
</div>
#if ($errors->has('country'))
<span class="help-block">
<strong>{{ $errors->first('country') }}</strong>
</span>
#endif
</div>
{{ Form::hidden('ip', $ip) }}
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
<?php
function get_ip_address() {
// check for shared internet/ISP IP
if (!empty($_SERVER['HTTP_CLIENT_IP']) && validate_ip($_SERVER['HTTP_CLIENT_IP'])) {
return $_SERVER['HTTP_CLIENT_IP'];
}
// check for IPs passing through proxies
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
// check if multiple ips exist in var
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false) {
$iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
foreach ($iplist as $ip) {
if (validate_ip($ip))
return $ip;
}
} else {
if (validate_ip($_SERVER['HTTP_X_FORWARDED_FOR']))
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
}
if (!empty($_SERVER['HTTP_X_FORWARDED']) && validate_ip($_SERVER['HTTP_X_FORWARDED']))
return $_SERVER['HTTP_X_FORWARDED'];
if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
return $_SERVER['HTTP_FORWARDED_FOR'];
if (!empty($_SERVER['HTTP_FORWARDED']) && validate_ip($_SERVER['HTTP_FORWARDED']))
return $_SERVER['HTTP_FORWARDED'];
// return unreliable ip since all else failed
return $_SERVER['REMOTE_ADDR'];
}
?>
All the values echoed with {{ }} are sent through the htmlentities function of PHP. If you want to skip that, than you should use another notation:
{!! $unescaped_html !!}
Source

Resources