KiRa
KiRa

Reputation: 924

Update method not saving

I have a simple crud application using Laravel 8. My saving code is perfectly working. But my update code is not working. My console is not returning any error. Did I miss something that causes it not to save?. Code

private $validations;

public function __constructor()
{
    $this->validations([
        'name' => 'required',
        'email' => "required|email",
        'phone' => "required",
        'dob' => "required|date",
        'interested_package' => 'sometimes'
    ]);
}
public function update(Request $request)
{
    $rules = $this->validations;
    $rules['id'] = 'required|exists:leads';
    $postData = $this->validate($request, $rules);
    Lead::where('id',  $postData['id'])->update($postData);
    return redirect()->route('lead.view', ['lead' =>  $postData['id']]);
}

Update

Here is my Table structure enter image description here

Upvotes: 0

Views: 96

Answers (3)

KiRa
KiRa

Reputation: 924

I solve it by changing the line Lead::where('id', $postData['id'])->update($postData); into Lead::findOrFail($postData['id'])->update($request->all());

I don't know the reason why it works that way.

Upvotes: 0

Prashant Deshmukh.....
Prashant Deshmukh.....

Reputation: 2292

Change this line Lead::where('id', $postData['id'])->update($postData); to this Lead::where('id', $postData['id'])->update($request->all());

Upvotes: 1

Mohammed Azar
Mohammed Azar

Reputation: 101

I think in constructor function you have to use $this->validations=[...]. Instead of $this->validations (...).

Upvotes: 0

Related Questions