Laravel UpdateUserRequest validation rule with user id

I have a User model, and this is my update() method in UserController:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public function update(Request $request, User $user)
{
// Assign authorization
Gate::authorize('update', $user);
$request->validate([
'username' => 'required|unique:users,username,' . $user->id,
// other rules ...
]);
// ...
}
</code>
<code>public function update(Request $request, User $user) { // Assign authorization Gate::authorize('update', $user); $request->validate([ 'username' => 'required|unique:users,username,' . $user->id, // other rules ... ]); // ... } </code>
public function update(Request $request, User $user)
{
    // Assign authorization
    Gate::authorize('update', $user);
    
    $request->validate([
        'username' => 'required|unique:users,username,' . $user->id,
        // other rules ...
    ]);
    
    // ...
}

I want to keep these validation rules in a separate UpdateUserRequest request:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class UpdateUserRequest extends FormRequest
{
public function authorize(): bool
{
return true; // Changed to 'true' to handle authorization logic in UserPolicy
}
public function rules(): array
{
return [
'username' => 'required|unique:users,username,' . $user->id, // <-- Problem
// other rules ...
];
}
}
</code>
<code>class UpdateUserRequest extends FormRequest { public function authorize(): bool { return true; // Changed to 'true' to handle authorization logic in UserPolicy } public function rules(): array { return [ 'username' => 'required|unique:users,username,' . $user->id, // <-- Problem // other rules ... ]; } } </code>
class UpdateUserRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true; // Changed to 'true' to handle authorization logic in UserPolicy
    }

    public function rules(): array
    {
        return [
            'username' => 'required|unique:users,username,' . $user->id, // <-- Problem
            // other rules ...
        ];
    }
}

The problem here is that $user is not defined. I have read elsewhere that using a route for this matter is a possible solution, but that’s not the proper way of achieving this task.

My question is what is the correct way to pass user information to UpdateUserRequest class?

7

My question is what is the correct way to pass user information to
UpdateUserRequest class?

I assumed you are using laravel 11.X and your route definition is…

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Route::put('/users/{user}', [UserController::class, 'update']);
</code>
<code>Route::put('/users/{user}', [UserController::class, 'update']); </code>
Route::put('/users/{user}', [UserController::class, 'update']);

In this scenario you can do it inside the UpdateUserRequest.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class UpdateUserRequest extends FormRequest
{
public function authorize(): bool
{
// Here you can also interact with your `gates` & `policy`,Check the docs link I provided below to get clear insights.
return true;
}
public function rules(): array
{
//Scenario 1
$user = User::find($this->route('user')); //If you are not using `implicit model binding`
//Scenario 2
$user = $this->user ; //If you are using `route-model-binding`
return [
'username' => 'required|unique:users,username,' . $user->id, // <-- "Issue will be solved"
// other rules ...
];
}
}
</code>
<code>class UpdateUserRequest extends FormRequest { public function authorize(): bool { // Here you can also interact with your `gates` & `policy`,Check the docs link I provided below to get clear insights. return true; } public function rules(): array { //Scenario 1 $user = User::find($this->route('user')); //If you are not using `implicit model binding` //Scenario 2 $user = $this->user ; //If you are using `route-model-binding` return [ 'username' => 'required|unique:users,username,' . $user->id, // <-- "Issue will be solved" // other rules ... ]; } } </code>
class UpdateUserRequest extends FormRequest
{
    public function authorize(): bool
    {
        // Here you can also interact with your `gates` & `policy`,Check the docs link I provided below to get clear insights.
        return true; 
    }

    public function rules(): array
    {
        //Scenario 1
        $user = User::find($this->route('user')); //If you are not using `implicit model binding`
        //Scenario 2
        $user = $this->user ; //If you are using `route-model-binding`


        return [
            'username' => 'required|unique:users,username,' . $user->id, // <-- "Issue will be solved"
            // other rules ...
        ];
    }
}

Then in Controller class import & type-hint the FormRequest class i.e. UpdateUserRequest on your Controller method that is missing in your current code.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public function update(UpdateUserRequest $request, User $user)
{
// Assign authorization
Gate::authorize('update', $user);
// Retrieve the validated input data...
$validated = $request->validated(); // it will by default validate all the input data you set up in the `rule()` of `UpdateUserRequest`
// ...
}
</code>
<code>public function update(UpdateUserRequest $request, User $user) { // Assign authorization Gate::authorize('update', $user); // Retrieve the validated input data... $validated = $request->validated(); // it will by default validate all the input data you set up in the `rule()` of `UpdateUserRequest` // ... } </code>
public function update(UpdateUserRequest $request, User $user)
{
    // Assign authorization
    Gate::authorize('update', $user);

    // Retrieve the validated input data...
     $validated = $request->validated();  // it will by default validate all the input data you set up in the `rule()` of `UpdateUserRequest`
    
    
    // ...
}

For more clear and in-depth insights please check the official-docs here.

15

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật