1

Want to updating the data using laravel. I make a function inside the model and using that method inside the controller but when calling the method its inserting the data with new id.function as following .

public function edit_client($req,$id) { 
    $client = new client(); 
    $client->find($id); 
    $client->name = $req->name; 
    $client->address = $req->address; 
    $client->email = $req->email; 
    $client->phone = $req->phone; 
    $client->gender = $req->gender; 
    $client->department = $req->department; 
    $client->update();}
9
  • Yes, because of $client = new client();.. remove or comment this line and try again. Commented Nov 14, 2016 at 12:41
  • Inserting new record. Commented Nov 14, 2016 at 12:43
  • 1
    Yes save() will insert new record. Commented Nov 14, 2016 at 12:44
  • with $client->update(); no changes and with $client->save(); inserting new record Commented Nov 14, 2016 at 12:44
  • 1
    Yes updated_at will be updated automatically Commented Nov 14, 2016 at 12:48

2 Answers 2

10

You can do this instead:

public function edit_client($req,$id) { 
    $client = Client::find($id); 
    $client->name = $req->name; 
    $client->address = $req->address; 
    $client->email = $req->email; 
    $client->phone = $req->phone; 
    $client->gender = $req->gender; 
    $client->department = $req->department; 
    $client->save();
}

Or use update() method:

public function edit_client($req,$id) { 
    $client = Client::where('id', $id)->update([
        'name' = $req->name,
        'address' = $req->address,
        'email' = $req->email,
        'phone' = $req->phone,
        'gender' = $req->gender,
        'department' = $req->department
    ]);
}

If you'll decide to use update() method, do not forget to add $fillable array.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks its working. one question ? function is in same model so may i use $this instead $client.
If you'll decide to use this code in the model, then yes you should use it like this: return $this->where('id', $id)->update(....);
may i use $this->save(); i mean without creating instance.
You can do this in model: $client = $this->find($id); $client->email = '[email protected]'; $client->save();
ok thanks, Will you please explain why i cant use $this- >find; $this->email = '[email protected]'; $this->save(); whats the reason?
2

try this example:

    $obj= Client::where('id', $id)->update([
        'name' = $req->name,
        'address' = $req->address,
        'email' = $req->email,
        'phone' = $req->phone,
        'gender' = $req->gender,
        'department' = $req->department
    ]);

1 Comment

Seriously? You checked my answer as best answer, in a day one of your friends copy-pasted my code without any changes and you took it away and checked his answer. Nice behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.