How do I create date in register form - laravel

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>

Related

Error "The PATCH method is not supported for this route. Supported methods: GET, HEAD, POST. " update method

Well hello there, I am trying to update a register of my table assistants, but when I push the button submit of the form, this error appear, I am using a table pivot between assistants and events, but i am only try to edit a assistant.This is the error
This is my code AssistantController.php file and the methods edit and update
edit and update methods
public function edit(Assistant $assistant)
{
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return view('assistants.edit',compact('assistant','event'));
}
public function update(Request $request, Assistant $assistant)
{
#updating
$assistant->update($request->all());
//$assistants->user()->associate(Auth::user());
//
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return redirect('assistants.index',compact('assistant','event'));
}
This is my form to assistants
input form assistants
#csrf
<div class="form-group">
<label for="id">Document:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('id') ?? $assistant->id }}" name="id" autofocus>
<div>{{ $errors->first('id') }}</div>
</div>
<div class="form-group">
<label for="name">Name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('name') ?? $assistant->name }}" name="name" placeholder="First Name assistant">
<div>{{ $errors->first('name') }}</div>
</div>
<div class="form-group">
<label for="last_name">Last name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('last_name') ?? $assistant->last_name }}" name="last_name" placeholder="Last name assistant">
<div>{{ $errors->first('last_name') }}</div>
</div>
<div class="form-group">
<label for="phone">Phone:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('phone') ?? $assistant->phone }}" name="phone">
<div>{{ $errors->first('phone') }}</div>
</div>
<div class="form-group">
<label for="email">Email:</label>
<small class="text-muted">Required(*)</small>
<input type="mail" class="form-control form-control-sm " value="{{ old('email') ?? $assistant->email }}" name="email">
<div>{{ $errors->first('email') }}</div>
</div>
<div class="form-group">
<label for="observations">Observations:</label>
<input type="text" class="form-control form-control-sm " value="{{ old('observations') ?? $assistant->observations }}"name="observations">
<div>{{ $errors->first('observations') }}</div>
</div>
This is my edit page
#extends('layouts.back')
#section('title','Edit assistants')
#section('content')
<div class="container p-4">
<div class="row">
<div class="card">
<div class="card-header">
<div class="card-title">
<h3>Edit details to assistant:</h3><br>
<h4><strong> {{$assistant->name}} {{$assistant->last_name}}</strong> </h4>
</div>
</div>
<div class="card-body">
<form action="/assistants" method="POST">
#method('PATCH')
#include('assistants.form')
<button type="submit" class="btn btn-primary">Update</button>
Cancel
</form>
</div>
<div class="card-footer">
</div>
</div>
</div>
</div>
#endsection
There are my routes
routes
Auth::routes();
Route::get('/', 'HomeController#index')->name('home');
Route::resource('events', 'EventController');
Route::resource('assistants', 'AssistantController');
Route::resource('certificates', 'CertificateController');
Route::resource('signers', 'SignerController');
I am beginner.
thank you all.
In your edit page, change your form action from
<form action="/assistants" method="POST">
to
<form action="/assistants/{{ $assistant->id }}" method="POST">
Without the assistant id in the action field, Laravel thinks you are trying to update the base assistants route, which is an invalid action for that route.

Route [/admin/update-pwd] not defined

I am trying to change the admin password but it says: Route [/admin/update-pwd] not defined. How do I change it?
controller:
public function changepassword()
{
return view('admin.changepassword');
}
public function chkPassword(Request $request){
$data = $request->all();
$adminCount = Admin::where(['username' => Session::get('adminSession'),'password'=>md5($data['current_pwd'])])->count();
if ($adminCount == 1) {
echo "true"; die;
} else {
echo "false"; die;
}
}
public function updatePassword(Request $request){
if($request->isMethod('post')){
$data = $request->all();
$adminCount = Admin::where(['username' => Session::get('adminSession'),'password'=>md5($data['current_pwd'])])->count();
if ($adminCount == 1) {
$password = md5($data['new_pwd']);
Admin::where('username',Session::get('adminSession'))->update(['password'=>$password]);
return redirect('/admin/settings')->with('flash_message_success', 'Password updated successfully.');
}else{
return redirect('/admin/settings')->with('flash_message_error', 'Current Password entered is incorrect.');
}
}
}
blade file:
<form method="POST" action="{{ route('/admin/update-pwd') }}" enctype="multipart/form-data">
#csrf
#method('PATCH')
<strong class="text-muted d-block mb-2">Enter Current Password</strong>
<div class="input-group mb-3">
<div class="input-group input-group-seamless">
<input type="password" name="current_pwd" class="form-control #error('password') is-invalid #enderror" id="form2-password" placeholder="Password">
<span class="input-group-append">
<span class="input-group-text">
<i class="material-icons">lock</i>
</span>
</span>
</div>
</div>
<strong class="text-muted d-block mb-2">Enter New Password</strong>
<div class="input-group mb-3">
<div class="input-group input-group-seamless">
<input type="password" name="new_pwd" class="form-control #error('password') is-invalid #enderror" id="form2-password" placeholder="Password">
<span class="input-group-append">
<span class="input-group-text">
<i class="material-icons">lock</i>
</span>
</span>
</div>
</div>
<strong class="text-muted d-block mb-2">Confirm New Password</strong>
<div class="input-group mb-3">
<div class="input-group input-group-seamless">
<input type="password" name="confirm_pwd" class="form-control #error('password') is-invalid #enderror" id="form2-password" placeholder="Password">
<span class="input-group-append">
<span class="input-group-text">
<i class="material-icons">lock</i>
</span>
</span>
</div>
</div>
#error('password')
<div class="small text-danger">{{ $message }}</div>
#enderror
<button type="submit" class="mb-2 btn btn-primary mr-2">Update Password</button>
</form>
Routes:
Route::get('/admin/changepassword', 'ChangepasswordController#changepassword');
Route::get('/admin/check-pwd','AdminController#chkPassword');
Route::match(['get', 'post'],'/admin/update-pwd','AdminController#updatePassword');
When you use the route helper it expects that you provide a route name not a URI, so add this:
Route::match(['get', 'post'],'/admin/update-pwd','AdminController#updatePassword')
->name('admin.update_password');
Then in your view you can use:
route('admin.update_password');
Or use the url helper method instead:
// instead of this
{{ route('/admin/update-pwd') }}
//use
{{ url('/admin/update-pwd') }}
You are trying to use Laravel named route inside your form method. Use Laravel Named Routed, and define it inside your form.
//inside web.php
Route::post('/admin/update-pwd','AdminController#updatePassword')->name('admin/update-pwd');
Then you will be able to use inside form
<form method="POST" action="{{ route('/admin/update-pwd') }}" enctype="multipart/form-data">

Editing user info using laravel

I've built a cms interface for the admin in my website. among other things the admin can add\edit users info using forms.
when I send the edit form I keep getting this error: Column not found: 1054 Unknown column 'updated_at' in 'field list' which suggests that the DB update is trying to save all of the request indexes (which contains values of columns from other table) and not just the one I'm trying to update.
I've manage to track the problem to one line $user_role->save();.
the lines above that do what their suppose to (finding thr correcct user_role and change its value).
Here is my code
Model
static public function update_user($request, $id){
$image_name = '';
if( !empty($request['profile_image']) && $request->hasFile('profile_image') && $request->file('profile_image')->isValid() ){
$file = $request->file('profile_image');
$image_name = date('Y.m.d.H.i.s') . '-' . $file->getClientOriginalName();
$request->file('profile_image')->move( public_path() . '/images/profile-images/' , $image_name);
$img = Image::make( public_path() . '/images/profile-images/' . $image_name );
$img->resize(370, null, function ($constraint) {
$constraint->aspectRatio();
});
$img->save();
}
$user = self::find($id);
$user->name = $request['name'];
$user->email = $request['email'];
$user->phone = $request['phone'];
if( !empty($request['password']) ){
$user->password = bcrypt($request['password']);
}
if(!empty($image_name)){
$user->profile_image = $image_name;
}
if( !empty($request['role_id']) ){
$user_role = Users_role::find($id);
$user_role->role_id = $request['role_id'];
$user_role->save();
}
$user->save();
Session::flash('sm', 'Your profile has been updated');
Session::flash('sm-position', 'toast-top-center');
Session::put('user_name', $request['name']);
}
View
<div class="row">
<div class="span9">
<div class="content">
<div class="module message">
<div class="module-head">
<h3><b>Edit Product</b></h3>
</div><br>
<div class="content">
<div class="module message">
<div class="module-body">
<form action="{{ url('cms/users/' . $user->id) }}" method="POST" novalidate="novalidate" autocomplete="off" enctype="multipart/form-data">
<div class="module-body">
#method('PUT')
#csrf
<input type="hidden" name="user_id" value="{{ $user->id}}">
<div class="form-group">
<div class="input-group mb-3">
<div class="w-100 field-input-cms">
<label for="category-id" class="input-group-text h-50"><span class="text-danger">*</span><b> Permissions:</b></label>
<select name="role_id" class="custom-select span-8">
<option #if ( $user->role_id == 8 ) selected="selected" #endif value="8">Admin</option>
<option #if ( $user->role_id == 2 ) selected="selected" #endif value="2">Regular</option>
</select>
</div>
<small class="text-muted help-text">Please select one option</small><br>
<span class="text-danger"> {{ $errors->first('category_id') }}</span>
</div>
<div class="w-100 field-input-cms">
<label for="name" class="input-group-text h-100"><span class="text-danger">*</span><b> Name:</b></label>
<input type="text" value="{{ $user->name }}" name="name" style="width:100%" class="form-control" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-default">
</div>
<small class="text-muted help-text">Name of user</small><br>
<span class="text-danger"> {{ $errors->first('name') }}</span>
<div class="field-input-cms w-100">
<label for="email" class="input-group-text"><span class="text-danger">*</span><b> Email:</b></label>
<input type="text" value="{{ $user->email }}" name="email" size="120" class="form-control mw-100" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-default">
</div>
<small class="text-muted text-balck help-text"> Email of user</small><br>
<span class="text-danger"> {{ $errors->first('email') }}</span>
<div class="field-input-cms w-100">
<label for="phone" class="input-group-text"><span class="text-danger">*</span><b> Phone:</b></label>
<input type="text" value="{{ $user->phone }}" name="phone" size="120" class="form-control mw-100" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-default">
</div>
<small class="text-muted text-balck help-text"> Phone number of user</small><br>
<span class="text-danger"> {{ $errors->first('phone') }}</span>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupFileAddon01">Upload</span>
</div>
<div class="custom-file">
<input type="file" name="profile_image" class="custom-file-input" id="inputGroupFile01" aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" name="profile_image" for="inputGroupFile01">Choose file</label>
</div>
</div>
<small class="text-muted help-text">Image must be: jpg, jpeg, png, gif. Max: 5mb</small><br>
<span class="text-danger"> {{ $errors->first('profile_image') }}</span>
</div>
<div class="form-group">
<img id="cms-profile-image" src="{{ asset('images/' . $user->profile_image) }}" >
</div><br>
<a class="btn btn-inverse" href="{{ url('cms/users') }}">Cancel</a>
<input type="submit" value="Save Product" name="submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
</div>
</div>
</div> <!--/.content-->
</div><!--/.span9-->
</div>
Image of the error gaven by laravel
I should mention that if I comment out this code:
if( !empty($request['role_id']) ){
$user_role = Users_role::find($id);
$user_role->role_id = $request['role_id'];
$user_role->update();
}
all the values are saved correctly.
If your users table doesn't have created_at and updated_at columns you should set:
public $timestamps = false;
in your User model.
Laravel by default assumes you have those fields for tables. So whenever record is created/updated it will automatically set/update those fields.
Alternatively you can update your table structure to add those fields and then those fields will be automatically handled by Laravel (in such case don't set timestamps to false).
You might be interested to read about Eloquent conventions

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

Resources