AmarjaPatil4
AmarjaPatil4

Reputation: 1650

Update user information in laravel 5

I am doing laravel project and there is 'lead' table containing fields lead_id,lead_name,email. There is a option for edit information in my project, When I am trying to edit lead information there may be a case that I don't want to edit the email address. My code is,

LeadRequest.php

public function rules()
{
    return [
        'lead_name' => 'required',
        'email' => 'required|email|unique:lead',
    ]; }

I have provided email as a unique so when I am trying to update profile without changing the email address, then it shows me an error message as 'email address has already taken'.

So I tried to apply the laravel validation rule 'Forcing A Unique Rule To Ignore A Given ID:' as follows,

'email' => 'unique:lead,email_address,'.$lead.'lead_id',

But how to provide value of $lead. Showing message as 'undefined variable'. My controller code is as below,

LeadController.php

public function update(LeadRequest $request, $id)
{

    $lead = Lead::findOrFail($id);
    $lead->update($request->all());
    return redirect('lead');
}

Please give any suggestion.

Upvotes: 3

Views: 839

Answers (2)

AmarjaPatil4
AmarjaPatil4

Reputation: 1650

Thanks for your support, I tried the follwing code in LeadRequest.php and this worked for me,

public function rules()
{
    return [
        'lead_name' => 'required',
        'email' =>  'required|email|unique:lead,email,NULL,id,lead_id,'.Auth::user()->id,]; }

You can refer the link, http://laravel.io/forum/03-11-2014-validation-unique-to-user

Upvotes: 2

fico7489
fico7489

Reputation: 8560

Look at my working example:

public function rulesUserEdit($id = NULL)
{
    return [
        'name' => 'required|min:3',
        'oib' => 'required|min:11|max:11|unique:users' . ($id ? ",oib,$id" : ''),
    ];
}

it should work for you. You need to pass user id in argument when user is updating. When user is creating pass NULL, or nothing because NULL is default argument.

Upvotes: 0

Related Questions