1

I want to make a quiz app in Laravel and I've got some error: array to string conversion

This is my controller:

public function validateForm1($request)
{
    return $this->validate($request, [
        'quiz_id'        => 'required',
        'question'       => 'required',
        'options'        => 'required|array',
        'options.*'      => 'required|string',
        'correct_answer' => 'required',
    ]);
}

public function buat(Request $request)
{
    $data = $this->validateForm1($request);
    $question = (new Pertanyaan)->storeQuestion($data);
    $Answer =  (new Jawaban)->storeAnswer($data,$question);    
               
    return redirect()->back()->with('message','Pertanyaan berhasil disimpan');        
}

This is my question model:

public function storeQuestion($data)
{           
    return Pertanyaan::create($data);
}

This is my answer model:

public function storeAnswer($data, $question)
{
    foreach ($data['options'] as $key => $option) {
        
        $is_correct = false;
       
        if ($key == $data['correct_answer']) {
            $is_correct = true;
        }

        $answer = Jawaban::create([
            'question_id' => $question->id,
            'answer'      => $option,
            'is_correct'  => $is_correct,
        ]);
    }
}
11
  • 1
    Can you please share the stack trace? Commented Jun 28, 2020 at 20:40
  • hi sir im new in laravel and programming.. what should i do for print out the stack trace ? Commented Jun 28, 2020 at 20:44
  • Stack trace is sequence of errors you’ll see on your browser while accessing your laravel application. Commented Jun 28, 2020 at 20:48
  • Array to string conversation can you show us the more details about this error? Like line number and actual error message. Commented Jun 28, 2020 at 20:50
  • 1
    I want to make a quiz app in Laravel and I've got some error: array to string conversion Alright, do you have a specific question? We can't debug your entire program for you. Please see How to Ask, help center. Commented Jun 28, 2020 at 22:24

3 Answers 3

1

You have to use like this

return $this->validate($request, [
    'quiz_id'        => 'required',
    'question'       => 'required',
    'options'        => 'required|array',
    'correct_answer' => 'required',
]);

Then your error remove automatically array to string conversion

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

Comments

0

this error i got when i use guarded in model, so i change it to fillable and it work properly. sorry if im not capturing all of my model.

after read the documentation about special variable eloquent, i lil understand. here the link i read but in indonesian leanguage. and sorry for my bad english sir. im new here

https://id-laravel.com/post/mengenal-eloquent-variable-spesial/

Comments

0

When I had this error, it was caused by an error in my Model. I had the table name formatted as an array, like protected $table = ['log'];. Correcting this to protected $table = 'log'; fixed the problem.

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.