Skip to content
Advertisement

laravel update database multiple records

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

Advertisement

Answer

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

use IlluminateSupportFacadesDB;


 DB::table('certificates')
    ->where('id', $id)
    ->update([
    'mark' => $request->mark,
    'name' => $request->name, 
    'email' => $request->email,
    'dob' => $request->dob,
    'country' => $request->country
]);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement