2

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?

6
  • What is the primary key of the table? And can you get $id of the multiple rows? How to distinguish schools? By user_id? Commented Nov 23, 2017 at 3:20
  • I am using a one to many relationship where I have many schools and I use user_id to distinguish them(act as a foreign key). The $id is to get the user_id and the primary key is the id Commented Nov 23, 2017 at 3:23
  • Do you mean one user can have multiple schools? Commented Nov 23, 2017 at 3:27
  • Yup, if possible i want to do something like $test['meta_key = school_name'] = implode(' , ', $request->School); Commented Nov 23, 2017 at 3:28
  • $object2 = qualification::find($id); This line implies $id is id of the table, not the user_id ?? Commented Nov 23, 2017 at 3:30

1 Answer 1

1

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');

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

2 Comments

It work, thanks a lot !! But could you explain to me what this case and switch thing is? It my first time seeing it
You're welcome. There's nothing special. The first line gets all rows of which user_id equals to the parameter. And for each row, according to the meta_key, appropriate meta_value is set and finally saved. That's all.

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.