0

I want to save all of these data which is might be an array, into my answer table. I have tried so many ways but cant seem to find a correct solution.. can someone helped me thank you. I dont how to store the data from the answer(create.blade.php) into a database which is on the store function in the AnswerController as I dont didn't understand how to use that $key=> $value stuff. Im still learning as I really put my heart on Laravel. Can someone who is Laravel's expertise help me or guide me? Thankyou so much..

Question Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Question extends Model
{
    public function feedback()
    {
        return $this->belongsTo('App\Feedback','feedback_id');
    }

    public function answers()
    {
        return $this->hasMany('App\Answer');
    }

}

Answer Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Answer extends Model
{
    protected $fillable = ['question_id', 'feedback_type', 'answer'];

    public function question()
    {
        return $this->belongsTo('App\Answer','question_id');
    }

   /* public function user()
    {
        return $this->belongsTo('App\Answer','user_id');
    } */
}

Answer (create.blade.php)

<form class="login100-form validate-form flex-sb flex-w" method="post" action="{{action('AnswerController@store')}}">
    <span class="login100-form-title p-b-53">
        Survey Page
    </span>

    @csrf,
    @foreach ($questions as $question)
        <input type="hidden" class="form-control" name="question[]" value="{{$question->id}}">
        <div class="form-group col-md-12">
            <div class="txt1">
                Soalan : {{$question->question_desc}}
            </div>
        </div>

        @if($question->question_type === 'rating')
            <div class="form-group col-md-12">
                <span class="txt1">
                    Jawapan :
                    <span class="rating">
                        <input id="rating5[{{$question->id}}]" type="radio" name="answer[{{$question->id}}]"
                               value="5">
                        <label for="rating5[{{$question->id}}]">5</label>
                        <input id="rating4[{{$question->id}}]" type="radio" name="answer[{{$question->id}}]"
                               value="4">
                        <label for="rating4[{{$question->id}}]">4</label>
                        <input id="rating3[{{$question->id}}]" type="radio" name="answer[{{$question->id}}]"
                               value="3">
                        <label for="rating3[{{$question->id}}]">3</label>
                        <input id="rating2[{{$question->id}}]" type="radio" name="answer[{{$question->id}}]"
                               value="2">
                        <label for="rating2[{{$question->id}}]">2</label>
                        <input id="rating1[{{$question->id}}]" type="radio" name="answer[{{$question->id}}]"
                               value="1">
                        <label for="rating1[{{$question->id}}]">1</label>
                    </span>
                 </span>
            </div>
        @elseif($question->question_type === 'option')
            <div class="form-group col-md-12">
                Jawapan :
                <input type="radio" name="answer[{{$question->id}}]" value="yes">
                <label>YES</label>
                <input type="radio" name="answer[{{$question->id}}]" value="no">
                <label>NO</label>
            </div>
        @elseif($question->question_type === 'text')
            <div class="form-group col-md-12">
                <span class="txt1">
                    Jawapan :
                    <textarea class="form-control input-sm" id="answer" rows="3" name="answer[{{$question->id}}]"></textarea>
                 </span>
            </div>
        @endif
    @endforeach
    <div class="container-login100-form-btn m-t-17">
        <button class="login100-form-btn">
            Submit
        </button>
    </div>
</form>

AnswerController

<?php

namespace App\Http\Controllers;

namespace App\Http\Controllers;
use App\Feedback;
use App\Answer;
use App\Question;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;

class AnswerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $questions = Question::all();
        return view('Answer.index',compact('questions'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $questions = Question::all();
        return view('Answer.create',compact('questions'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       foreach ($request->question as $key => $value){
           $answer = new Answer();
           $answer -> question_id = $value;
           $answer -> answer = $request->input('answer');
           $answer -> save();
       }

        return Redirect::back();
    }

Route

<?php

Route::resource('feedbacks','FeedbackController');
Route::resource('questions','QuestionController');
Route::resource('answers','AnswerController');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
2
  • With you saying you don't understand the "$key => $value stuff" are you saying you're not use to using foreach loops or is it something else? Commented May 6, 2019 at 9:04
  • I know how to use foreach in order to display the data, but in order to set a key in it, I don't know how to do it. Commented May 6, 2019 at 15:55

1 Answer 1

1

The major problem is in your controller when you get the answer from request:

$answer -> answer = $request->input('answer');

The request contains an array of answers by question_id as the key. So you can get the related answer by:

$answer -> answer = $request->input('answer.'.$value);

**The key point is Laravel uses dot notation for getting array posts.

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

2 Comments

Thank you so much sir! May i know in the $value attribute, what data does it store it in? Is it a question id?
Your welcome. I am not sure $request->question would give you the array of questions has sent. But using $questions = Input::get('question'); works fine. It would give the array you can iterate by just like you want.

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.