Dkna
Dkna

Reputation: 437

Update data in database with laravel

Just want to check if it is possible to edit/update a data when my database is save the data like this: enter image description here

I want to update the school_name, start_date, end_date, qualification_list all those but I don't know how. Previously I did an update and it was successfull but the style inside is different: enter image description here

Can anyone give me any tips or what to do to be able to update the data for the first screenshot? Thanks in advance.

Previously I update my data like this:

public function update1(Request $request, $user_id){

    $object2 = qualification::find($user_id);
    $test = array();

$test['School'] = implode(' , ', $request->School);

$test['SDate'] = implode(' , ', $request->SDate);
$test['EDate'] = implode(' , ', $request->EDate);
$test['qualification'] = implode(' , ', $request->qualification);
    $object2->update($test);
    return redirect('/home');

}

But how do I update the data now based on the first screenshot?

Upvotes: 2

Views: 4299

Answers (1)

Miron
Miron

Reputation: 1067

If one user can have only one school and you can get user_id, the following code works.

public function update2(Request $request, $user_id){

    $rows = qualification::where('user_id', $user_id)->get();

    foreach ($rows as $row){
        switch ($row['meta_key']){
            case 'school_name':
                $row['meta_value'] = implode(' , ', $request->School);
                break;
            case 'start_date':
                $row['meta_value'] = implode(' , ', $request->SDate);
                break;
            case 'end_date':
                $row['meta_value'] = implode(' , ', $request->EDate);
                break;
            case 'qualification_list':
                $row['meta_value'] = implode(' , ', $request->qualification);
                break;
        }
        $row->save();
    }

    return redirect('/home');

}

Upvotes: 1

Related Questions