i have problem, when i want to edit my profile in laravel. When i click button update profile have this error :
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PATCH method is not supported for this route. Supported methods: GET, HEAD.
http://127.0.0.1:8000/profile
edit.blade.php
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
Update Profile
</div>
<div class="card-body">
<form method="POST" action="{{ route('profile.edit') }}">
#method('patch')
#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', $user->name) }}" 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="pseudo" class="col-md-4 col-form-label text-md-right">{{ __('pseudo') }}</label>
<div class="col-md-6">
<input id="pseudo" type="text" class="form-control #error('pseudo') is-invalid #enderror" name="pseudo" value="{{ old('pseudo', $user->pseudo) }}" autocomplete="pseudo" autofocus>
#error('pseudo')
<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', $user->email) }}" autocomplete="email">
#error('email')
<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">
Update Profile
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
web.php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/chats', 'ChatController#index')->name('chats');
Route::get('/messages', 'ChatController#fetchAllMessages');
Route::get('/messages', 'ChatController#sendMessage');
Route::get('/contacts', 'ContactsController#get');
Route::get('/conversation/{id}', 'ContactsController#getMessagesFor');
Route::get('/conversation/send', 'ContactsController#send');
Route::group(['middleware' => 'auth'], function () {
Route::get('profile', 'ProfileController#edit')->name('profile.edit');
});
Profile controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
/**
* Show the update profile page.
*
* #param Request $request
* #return \Illuminate\Contracts\Support\Renderable
*/
public function edit(Request $request)
{
return view('profile.edit', [
'user' => $request->user()
]);
}
}
someone can help resolve this error pls. I don't understand what is the problem.
passwordChange.blade.php i created this page for try if change password worked and in other page it's worked but when i try in one page in edit profile, dont worked.
#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">Laravel - Change Password with Current</div>
<div class="card-body">
<form method="POST" action="{{ route('profile') }}">
#csrf
#foreach ($errors->all() as $error)
<p class="text-danger">{{ $error }}</p>
#endforeach
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">Current Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="current_password" autocomplete="current-password">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">New Password</label>
<div class="col-md-6">
<input id="new_password" type="password" class="form-control" name="new_password" autocomplete="current-password">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">New Confirm Password</label>
<div class="col-md-6">
<input id="new_confirm_password" type="password" class="form-control" name="new_confirm_password" autocomplete="current-password">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
Update Password
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
When i try to do this in differents pages it's worked, i created other page changePassword.blade.php and when i change password in this page its worked, and when i try update profile when i leave password route etc... its worked too, but when i want to change all in one page i have this error
Facade\Ignition\Exceptions\ViewException
Undefined variable: user (View: /home/mokoch/Bureau/projetabonnementpayant/resources/views/profile/edit.blade.php)
http://127.0.0.1:8000/profile
If someone can help me resole this error
This line in your route, says it is a GET request only
Route::get('profile', 'ProfileController#edit')->name('profile.edit');
Your form says method="POST"
you can change your Route to "any", that will allow get and post
Route::any('profile', 'ProfileController#edit')->name('profile.edit');
The issue is in your form you are telling Laravel that it is a patch request with this line
#method('patch')
But in your routes file you are only looking for a get method
Route::get('profile', 'ProfileController#edit')->name('profile.edit');
If you are sending as a patch then you would need an additional line
Route::patch('profile', 'ProfileController#update');
Then you would need to create an update method in your controller to handle the save logic
public function update(Request $request)
{
// Logic to update
}
Related
I can't login using an account that I registered in Laravel app. I'm not getting an error,login page just reload after I press login button. But if I register a new account everything works good. I get logged in immediately after I registered account and if I logout I can't loggin again. No error or something, just login page reloading and that's it. How should I debug what's wrong or maybe you guys have any others hints?
Login controller:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
Login blade:
#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">{{ __('Prisijungimas') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('login') }}">
#csrf
<div class="form-group row">
<label for="username" class="col-md-4 col-form-label text-md-right">{{ __('Vartotojo vardas') }}</label>
<div class="col-md-6">
<input id="username" type="username" class="form-control #error('username') is-invalid #enderror" name="username" required 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">{{ __('Slaptažodis') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required >
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Prisijungti') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
I found the solution.
Just need to overwrite login controller to return username instead of email.
public function username(){
return 'username';
}
}
Thanks to #lagbox for the hint.
I've got problem with my laravel 8..
I Created Controller for edit profile, and when i access the page, it return view blank page or white screen with no error..
Here's my Controller
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
public function edit(Request $request)
{
return view('profile.edit', [
'user' => $request->user()
]);
}
}
Here's my route or web.php (Route for edit profile is at last)
Route::group(['prefix' => 'dashboard', 'middleware' => ['auth']], function () {
Route::get('/', [App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard.index');
Route::get('/categories/select', [App\Http\Controllers\CategoryController::class, 'select'])->name('categories.select');
Route::resource('/categories', App\Http\Controllers\CategoryController::class);
Route::get('/tags/select', [App\Http\Controllers\TagController::class, 'select'])->name('tags.select');
Route::resource('/tags', App\Http\Controllers\TagController::class);
Route::resource('/posts', App\Http\Controllers\PostController::class);
Route::resource('/banners', App\Http\Controllers\BannerController::class);
Route::resource('/clients', App\Http\Controllers\ClientController::class);
Route::resource('/gallerys', App\Http\Controllers\GalleryController::class);
Route::resource('/products', App\Http\Controllers\ProductController::class);
Route::resource('/services', App\Http\Controllers\ServiceController::class);
Route::resource('/teams', App\Http\Controllers\TeamController::class);
Route::resource('/testimonies', App\Http\Controllers\TestimonyController::class);
Route::resource('/metas', App\Http\Controllers\MetaController::class);
Route::resource('/keywords', App\Http\Controllers\KeywordController::class);
Route::group(['prefix' => 'filemanager'], function () {
Route::get('/index', [App\Http\Controllers\FileManagerController::class, 'index'])->name('filemanager.index');
\UniSharp\LaravelFilemanager\Lfm::routes();
});
Route::group(['middleware' => 'auth'], function () {
Route::get('/profile', [App\Http\Controllers\ProfileController::class, 'edit'])->name('profile.edit');
});
});
And here's my view
#extends('layouts.dashboard')
#section('title')
Edit Profile
#endsection
#section('breadcrumbs')
{{ Breadcrumbs::render('edit_profile') }}
#endsection
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
Update Profile
</div>
<div class="card-body">
<form method="POST" action="{{ route('profile.update') }}">
#method('patch')
#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', $user->name) }}" 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="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', $user->username) }}" 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="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', $user->email) }}" autocomplete="email">
#error('email')
<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">
Update Profile
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
I still work this at localserver, anyone know where is the problem?
I think there is issue in your controller code. Please try below code. I have changed user fetch syntax.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
public function edit(Request $request)
{
return view('profile.edit', [
'user' => \Auth::user()
]);
}
}
Hi i am actually new in laravel , and learning , i have been assigned a task to do and i had been working a lot but i am really stuck i need some help , this is the Admin Dashboard in which i am trying to create a form which submit the values of the form shown in the picture , from the view to the route and controller where controller uses a create function to add all those values in the database mysql
The Codes for Home View :
#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">{{ __('Dashboard') }}</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
{{ __('You are logged in') }} {{$user}} ..!
</div>
<div class="card-body">
<div class="card-body">
<form method="POST" action="{{route('home.create')}}">
#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">
{{ __('Adduser') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
The Code for My Route in web.php is here:
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
route::get('/admin/user/roles',['middleware'=>['role','auth','web'],function(){
return "Middleware role";
}]);
route::get('/home','AdminController#index');
//route::post('/home/{name}/email/password/confirmpassword','HomeController#create');
Route::post('home', 'HomeController#create')->name('home.create');
and here's the HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index(Request $request)
{
return view('home');
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
Please tell me where am i making mistake cuz i tried a lot of things but it never worked :( always giving error
Try public function to submit form data & use Request instead of array
public function create(Request $request)
{
return User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
}
Laravel handles the parameters passed through GET or POST methods itself.
*. You must use Illuminate/Http/Request in your controller, exactly like the one used in index function. You don't need to use Request $request in index if you're just showing the view.
Instead you should use it in create.
And for using it:
public function create(Request $request)
{
$data = $request->validate([
// validation rules
]);
$user = User::create($data);
// .. anything you want
}
Be aware you're still able to use each parameter with $request->nameOfParameter
Its name is the name that you passed through HTML form.
Hope to be useful.
Doc: Laravel 8 Request Documentation
Error messages aren't displaying when i enter wrong credentials.
I made my own routes for authentication for login.
// Auth::routes(); //Commented this out
// Authentication Start
Route::get('/login', 'Auth\LoginController#showLoginForm')->name('auth.login.get');
Route::post('submit-login', 'Auth\LoginController#login')->name('auth.login.post');
Route::post('/logout', 'Auth\LoginController#logout')->name('logout');
Route::get('/register', 'Auth\RegisterController#showRegistrationForm')->name('auth.register.get');
Route::post('submit-register', 'Auth\RegisterController#register')->name('auth.register.post');
// Authentication End
This is the ajax part
window.getLoginPage = function(){
$.ajax({
type:'GET',
url:'/login',
success:function(response) {
$('.dynamicAuthContent').html('');
$('.dynamicAuthContent').append(response);
$('#myModal').modal('show');
$('.modalBtns').removeClass('activeAuth');
$('.signInModal').addClass('activeAuth');
}
});
}
and this is the modal body
<div class="modal-body">
<div class="dynamicAuthContent"></div>
</div>
This is login.blade.php
<form method="POST" action="{{ route('auth.login.post') }}">
#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="bColor form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" 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="bColor form-control #error('password') is-invalid #enderror" name="password" 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="bColor 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 contactSubmitBtn">
{{ __('Login') }}
</button>
#if (Route::has('password.request'))
<a class="btn btn-link" href="{{ route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
#endif
</div>
</div>
</form>
Everything is working fine, but my errors arent showing, when i enter wrong credentials.
But when i visit the page directly, not through ajax, error messages are visible.
I think my error messages are coming, but the modal is getting closed. How can i avoid that?
You can't use the blade error bag with ajax because they will only get rendered when the error variable is set so in case of ajax you must call the error function same as you called the success function in ajax and in side error function you can add new DOM element to show the errors, comming .
i created 2 pages. First it's edit profile and second for change password, this pages and functionnality worked. when the password change and the other inputs are on separate pages all work and all this changes. but when i try to put the password change on the profile edit i get this error:
Facade\Ignition\Exceptions\ViewException
Undefined variable: user (View: /home/mokoch/Bureau/projetabonnementpayant/resources/views/profile/edit.blade.php)
http://127.0.0.1:8000/profile
MatchOldPassword.php
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Hash;
class MatchOldPassword implements Rule
{
/**
* Determine if the validation rule passes.
*
* #param string $attribute
* #param mixed $value
* #return bool
*/
public function passes($attribute, $value)
{
return Hash::check($value, auth()->user()->password);
}
/**
* Get the validation error message.
*
* #return string
*/
public function message()
{
return 'The :attribute is match with old password.';
}
}
edit.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">
Update Profile
</div>
<div class="card-body">
<form method="POST" action="{{ route('profile.edit') }}">
#method('patch')
#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', $user->name) }}" 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="firstname" class="col-md-4 col-form-label text-md-right">{{ __('firstname') }}</label>
<div class="col-md-6">
<input id="firstname" type="text" class="form-control #error('firstname') is-invalid #enderror" name="firstname" value="{{ old('firstname', $user->firstname) }}" autocomplete="firstname" autofocus>
#error('firstname')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="address" class="col-md-4 col-form-label text-md-right">{{ __('address') }}</label>
<div class="col-md-6">
<input id="address" type="text" class="form-control #error('address') is-invalid #enderror" address="address" value="{{ old('address', $user->address) }}" autocomplete="address" autofocus>
#error('address')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="city" class="col-md-4 col-form-label text-md-right">{{ __('city') }}</label>
<div class="col-md-6">
<input id="city" type="text" class="form-control #error('city') is-invalid #enderror" city="city" value="{{ old('city', $user->city) }}" autocomplete="city" autofocus>
#error('city')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-md-4 col-form-label text-md-right">{{ __('phone') }}</label>
<div class="col-md-6">
<input id="phone" type="text" class="form-control #error('phone') is-invalid #enderror" phone="phone" value="{{ old('phone', $user->phone) }}" autocomplete="phone" autofocus>
#error('phone')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="profile_image" class="col-md-4 col-form-label text-md-right">{{ __('profile_image') }}</label>
<div class="col-md-6">
<input id="profile_image" type="text" class="form-control #error('profile_image') is-invalid #enderror" profile_image="profile_image" value="{{ old('profile_image', $user->profile_image) }}" autocomplete="profile_image" autofocus>
#error('profile_image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="zipcode" class="col-md-4 col-form-label text-md-right">{{ __('zipcode') }}</label>
<div class="col-md-6">
<input id="zipcode" type="text" class="form-control #error('zipcode') is-invalid #enderror" zipcode="zipcode" value="{{ old('zipcode', $user->zipcode) }}" autocomplete="zipcode" autofocus>
#error('zipcode')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="pseudo" class="col-md-4 col-form-label text-md-right">{{ __('pseudo') }}</label>
<div class="col-md-6">
<input id="pseudo" type="text" class="form-control #error('pseudo') is-invalid #enderror" pseudo="pseudo" value="{{ old('pseudo', $user->pseudo) }}" autocomplete="pseudo" autofocus>
#error('pseudo')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="birthday" class="col-md-4 col-form-label text-md-right">{{ __('birthday') }}</label>
<div class="col-md-6">
<input id="birthday" type="date" class="form-control #error('birthday') is-invalid #enderror" birthday="birthday" value="{{ old('birthday', $user->birthday) }}" autocomplete="birthday" autofocus>
#error('birthday')
<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', $user->email) }}" 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">Current Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="current_password" autocomplete="current-password">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">New Password</label>
<div class="col-md-6">
<input id="new_password" type="password" class="form-control" name="new_password" autocomplete="current-password">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">New Confirm Password</label>
<div class="col-md-6">
<input id="new_confirm_password" type="password" class="form-control" name="new_confirm_password" autocomplete="current-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">
Update Profile
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
changePassword.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">Laravel - Change Password with Current</div>
<div class="card-body">
<form method="POST" action="{{ route('profile') }}">
#csrf
#foreach ($errors->all() as $error)
<p class="text-danger">{{ $error }}</p>
#endforeach
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">Current Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="current_password" autocomplete="current-password">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">New Password</label>
<div class="col-md-6">
<input id="new_password" type="password" class="form-control" name="new_password" autocomplete="current-password">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">New Confirm Password</label>
<div class="col-md-6">
<input id="new_confirm_password" type="password" class="form-control" name="new_confirm_password" autocomplete="current-password">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
Update Password
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
ChangePasswordController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Rules\MatchOldPassword;
use Illuminate\Support\Facades\Hash;
use App\User;
class ChangePasswordController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('profile.edit');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function store(Request $request)
{
$request->validate([
'current_password' => ['required', new MatchOldPassword],
'new_password' => ['required'],
'new_confirm_password' => ['same:new_password'],
]);
User::find(auth()->user()->id)->update(['password'=> Hash::make($request->new_password)]);
dd('Password change successfully.');
}
}
Web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::group(['middleware' => 'auth'], function () {
Route::any('profile', 'ProfileController#edit')->name('profile.edit');
});
Route::group(['middleware' => 'auth'], function () {
Route::get('profile', 'ProfileController#edit')
->name('profile.edit');
Route::patch('profile', 'ProfileController#update')
->name('profile.update');
Route::get('profile', 'ChangePasswordController#index');
Route::post('profile', 'ChangePasswordController#store');
});
PasswordController.php
<?php
namespace App\Http\Controllers;
class PasswordController extends Controller
{
/**
* #return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit()
{
return view('profile.edit');
}
}
UpdateProfileRequest.php controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UpdateProfileRequest extends Controller
{
/**
* Update user's profile
*
* #param UpdateProfileRequest $request
* #return \Illuminate\Contracts\Support\Renderable
*/
public function update(UpdateProfileRequest $request)
{
$request->user()->update(
$request->all()
);
return redirect()->route('profile.edit');
}
}
UpdateProfileRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
class UpdateProfileRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return Auth::check();
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'name' => [
'required', 'string', 'max:255'
],
'username' => [
'required', 'string', 'max:255',
Rule::unique('users', 'username')->ignore(Auth::user()->id)
],
'email' => [
'required', 'email', 'max:255',
Rule::unique('users', 'email')->ignore(Auth::user()->id)
],
];
}
}
Someone can help me leave this error?
Error is pretty straight forward, You are using an undefined variable.
You can either add the variable:
public function edit()
{
return view('profile.edit', ['user' => auth()->user()]);
}
Or inside your blade use auth()->user() directly instead of $user