0

Question is I want to store that array to database to like this and topics can be more or less.

Database Choice table

|----|---------|----------|------------------|
| id | user_id | topic_id | question_number  |
|----|---------|----------|------------------|
|  1 |   1     |1st array |        21        |
|----|---------|----------|------------------|
|  2 |   1     |2nd array |        5         |
|----|---------|----------|------------------|
|  3 |   1     |3rd array |        3         |
|----|---------|----------|------------------|
..............................................
| 34 |   1     |34  array |        5         |
|----|---------|----------|------------------|

dd

array:3 [▼
"_token" => "HhR1wmjgJUvmnRsoYxcg3yezqzq7ORKqX6dAHzCD"
"time" => "30:01"
"number" => array:34 [▼
1 => "21"
2 => "5"
3 => "3"
4 => "0"
5 => "0"
6 => "0"
........
34 => "5"
]

Controller store function

public function store(Request $request){
    $time = $request->input('time');
    foreach ($request->input('number') as $key => $value) {
        Choice::create([
            'user_id' => Auth::id(),
            'time'  => $time,
            'topic_id' => $key,
        ]);
}}
2
  • do you get any error? Commented Jun 6, 2018 at 7:14
  • No. because i didn't found how to take arrays datas. but I'm still searching Commented Jun 6, 2018 at 7:15

1 Answer 1

2
public function store(Request $request) {
    $time = $request->input('time');
    foreach ($request->input('number') as $key => $value) {
        Choice::create([
            'user_id' => Auth::id(),
            'time'  => $time,
            'topic_id' => $key, // or  $key . 'st array';
            'question_number' => $value,
        ]);
    }
}}
Sign up to request clarification or add additional context in comments.

4 Comments

what is dd($request->input('number'));
Of course. i was just waiting 10 minutes. for accept your answer.
Thanks. You whant save 'topic_id' => 1, or 'topic_id . '1st array'?;
No I can handle that. I guess.

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.