With laravel7, I want to allow each user to be able to modify their profile, but I can't. With my code when the connected user wants to modify his profile there is a message telling him that he is not authorized. Which is not normal. I do not know where my error lies. Please help me!
Routes:
route::get('/profiles/{user}', 'ProfileController@show')->name('profiles.show');
route::get('/profiles/{user}/edit', 'ProfileController@edit')->name('profiles.edit');
route::patch('/profiles/{user}', 'ProfileController@update')->name('profiles.update');
My update 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 alert-primary">{{ __('Update my profile') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('profiles.update', ['user' => $user]) }}" >
@csrf
@method('PATCH')
<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}}" required 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="lastname" class="col-md-4 col-form-label text-md-right">{{ __('Lastname') }}</label>
<div class="col-md-6">
<input id="lastname" type="text" class="form-control @error('lastname') is-invalid @enderror" name="lastname" value="{{ old('lastname') ?? $user->lastname}}" required autocomplete="lastname" autofocus>
@error('lastname')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="role" class="col-md-4 col-form-label text-md-right">{{ __('Role') }}</label>
<div class="col-md-6">
<input id="role" type="text" class="form-control @error('role') is-invalid @enderror" name="role" value="{{ old('role') ?? $user->role}}" required autocomplete="role" autofocus>
@error('role')
<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" name="phone" value="{{ old('phone') ?? $user->phone}}" required 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="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}}" 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="adress" class="col-md-4 col-form-label text-md-right">{{ __('Adress') }}</label>
<div class="col-md-6">
<input id="adress" type="text" class="form-control @error('adress') is-invalid @enderror" name="adress" value="{{ old('adress') ?? $user->adress}}" required autocomplete="adress" autofocus>
@error('adress')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="profession" class="col-md-4 col-form-label text-md-right">{{ __('Profession') }}</label>
<div class="col-md-6">
<input id="profession" type="text" class="form-control @error('profession') is-invalid @enderror" name="profession" value="{{ old('profession') ?? $user->profession}}" required autocomplete="profession" autofocus>
@error('profession')
<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">
{{ __('Update') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\User;
use Validator;
use Illuminate\Foundation\Validation\ValidatesRequests;
class ProfileController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(User $user)
{
//
return view ('profiles.show', compact ('user'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(User $user)
{
//
$this->authorize('update', $user->profile);
return view ('profiles.edit', compact ('user'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update( User $user)
{
//
$this->authorize ('update', $user->profile);
$data = request()->validate ([
'firstname'=>'required',
'Lastname'=>'required',
'phone' => 'required' ,
'email' => 'required',
'adress' => 'required',
'profession' => 'required',
'password' => 'required',
]);
auth()->$user->update($data);
return redirect()->route('profiles.show', ['user'=> $user]);
}
}
My model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
protected $guarded = [];
public function user(){
return $this->belongsTo('App\User');
}
}
thanks
Grab your Routes inAuth
Middleware.
Route::group(['middleware' => ['auth']], function () {
route::get('/profiles/{user}', 'ProfileController@show')->name('profiles.show');
route::get('/profiles/{user}/edit', 'ProfileController@edit')->name('profiles.edit');
route::patch('/profiles/{user}', 'ProfileController@update')->name('profiles.update');
});
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Foundation, similar to Bootstrap, has become very popular as a more complex framework with some advanced but easy-to-implement CSS components. It is built with Sass, so just like Bootstrap, it is customizable. In addition to this, it also boasts some features that help make the design mobile responsive.
https://get.foundation/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.