3

I am new to Laravel. I'm simply trying to update a full row of mysql table. Data are being sent from a form. I only get to have only 2 records updated and the rest go NULL. I am sure that the fields names from Controller are matching input fields names in the form, also in the mysql table. I also made sure that fields listed in "fillable" method in the model.

In the controller tried:

public function approve($id, Request $request)
{
    $name = $request->input('name');
    $mark = $request->input('mark');
    $email = $request->input('email');
    $dob = date('Y-m-d H:i:s', strtotime($request->input('dob')));
    $country = $request->input('country');
     
    Certificate::where('id', '=', $id)
        ->update([
            'mark' => $mark,
            'name' => $name, 
            'dob' => $dob,
            'country' => $country
        ]);
}

I also tried

$query = DB::update("UPDATE certificates SET name='$name', mark='$mark', dob='$dob', country='$country' WHERE id='$id'");

Plz let me know what I might be doing wrong. Thank you

6
  • are you sure, you receive all fields from form, and you don't have validation? Commented Dec 15, 2020 at 23:33
  • yes, no validation for now Commented Dec 15, 2020 at 23:33
  • you are updating with where clause. You will update just the records that satisfy the where clause. Commented Dec 15, 2020 at 23:35
  • yes that what im trying to do, but on row id = 2 i dont get to have all records updated on that row Commented Dec 15, 2020 at 23:37
  • Does $id equals 2 when you call the function? Commented Dec 15, 2020 at 23:49

1 Answer 1

3

use DB facade, I was having an error similar to this, so this is what I used

use Illuminate\Support\Facades\DB;


 DB::table('certificates')
    ->where('id', $id)
    ->update([
    'mark' => $request->mark,
    'name' => $request->name, 
    'email' => $request->email,
    'dob' => $request->dob,
    'country' => $request->country
]);
Sign up to request clarification or add additional context in comments.

Comments

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.