4

I created a rule to check if email already exist only if the field was changed.

$rules = [
    'user-name'      => 'required',
    'user-email'     => [
        'required',
        'email',
        Rule::unique('users', 'email')->ignore(ID)
    ],
];

But I need to inform the user ID. How can I grab this information?

Route::resource('users', 'UsersController')->middleware('permission:create users');
marcelo2605
  • 2,734
  • 4
  • 29
  • 55

3 Answers3

14

In your Form request you have access to the authenticated user, so you can use:

$this->user()->id

--- Edit

getting the id from the url:

request()->route('id')

-- Second try

$this->route('user')

Or print out using php artisan route:list and whatever is used on the url as {user} or {id} that's what you need to use.

nakov
  • 13,938
  • 12
  • 60
  • 110
  • This return the ID of the logged user. I need to get the user ID whose information is being edited: http://website.test/users/2/edit – marcelo2605 Mar 14 '19 at 17:14
  • `request()->route('id')` return empty on log. – marcelo2605 Mar 14 '19 at 17:17
  • 1
    I see now you are using resource, so try `$this->route('user')`. Or print out using `php artisan route:list` and whatever is used on the url as `{user}` or `{id}` that's what you need to use. – nakov Mar 14 '19 at 17:20
  • Fixed! If you edit your answer, I will mark it as the solution. – marcelo2605 Mar 14 '19 at 17:24
  • 1
    For anyone using Resources in your routes file, it's worth noting that a hyphenated route i.e. `my-model` will become `my_model` when you are looking for the parameters, i.e. `$this->route('my_model')` – party-ring Oct 20 '20 at 09:45
1

Try this code:

$id = app('request')->segment(2);

I'm not sure, but maybe it will work:

$id = app('request')->user_id;
Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55
1

Please try this code. I'm using this code to get request-id when update data, and it works for me.

$id = $this->route('params_name');
The_spider
  • 1,202
  • 1
  • 8
  • 18
Mohammad Rana
  • 29
  • 1
  • 3