Laravel 8 url segment always return null

530 Views Asked by At

I created a validation for the creation and editing forms using Laravel's Request.

But I use the same form for creating and editing. So I created an exception so that when the url id is equal to the id registered in the database, it is allowed to insert the field that is configured as unique.

As you can see I'm using the segment to get the id that is being edited from the url, but it always returns null

public function rules()
{
    $id = $this->segment(3);

    return [
        'name' => "required|min:3|max:255|unique:companies,name,{$id},id",
        'image' => 'nullable|min:3|max:255',
    ];
}

How can I get this id from the url? what am I doing wrong? In the Laravel 6 version I used the same method to get the id and I had no problems

2

There are 2 best solutions below

0
Rateb Habbab On

You may try:

$id = Request::segment(3);
0
Daniel Drummond On

Thanks for the help, but I've solved the issue. The domain was considered as a segment of the url and we cannot consider it.

So the correct thing is

$id = $this->segment(2);