'Make survey at laravel 5.4 and MySQL

I need to make a survey for my website. I have this:

Route:

Route::resource('survey','surveyController');

Controller:

public function index()
{
    $show=survey_title::find(1);
    return view('survey',compact('show'));
}

Here we just read first survey.

Migrations:

Schema::create('survey_titles', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->timestamps();
    });

    Schema::create('survey_questions', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('survey_title_id')->unsigned()->index();
        //$table->integer('user_id')->unsigned()->index();
        $table->foreign('survey_title_id')->references('id')->on('survey_titles')->onDelete('cascade')->onUpdate('cascade');
       // $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
        $table->string('answer');
        $table->integer('voted')->nullable();
        $table->timestamps();
    });

Models:

survey_question:

public function survey_titles(){
    return $this->hasOne('App\survey_title');
}

public function users(){
      return $this->hasOne('App\User');
  }

survey_title:

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

user:

public function survey_questions(){
    return $this->hasOne('App\survey_question');
}

View:

 <div class="col-md-8 col-md-offset-2">
            
            <table class="table table-hover table-bordered table-striped">
                <thead>
                <tr>
                    <th>Voted by 0 People </th>
                    <th>{{$show->title}}</th>
                    <th>Question ID: {{$show->id}}</th>
                </tr>
                <tr>
                    <th>Check</th>
                    <th>Answer</th>
                    <th>ID</th>
                </tr>
                </thead>
                <tbody>
        @foreach($show->survey_questions as $ans)
                    <tr>
                        <td><input type="radio" name="check[]"></td>
                        <td>{{$ans['answer']}}</td>
                        <td>{{$ans['id']}}:</td>
                    </tr>
         @endforeach
                </tbody>
            </table>
            <a class="btn btn-primary" href="{{action('surveyController@update',$ans->id)}}">Vote</a>
        </div>

Now I put data manually in database and I retrieve.

When click on my 'Vote' I want it to send my Answer Id but instead it always sends last ID from table.

What's wrong?



Solution 1:[1]

This is happening because the:

<a class="btn btn-primary" href="{{action('surveyController@update',$ans->id)}}">Vote</a>

is out of the foreach loop, so after the loop is finished, the last id is being set to the link.

Put the href link inside the foreach loop, so it generates a link for each answer

<tr>
    <th>Voted by 0 People </th>
    <th>{{$show->title}}</th>
    <th>Question ID: {{$show->id}}</th>
    <th>Vote</th>
</tr>

@foreach($show->survey_questions as $ans)
    <tr>
        <td><input type="radio" name="check[]"></td>
        <td>{{$ans['answer']}}</td>
        <td>{{$ans['id']}}:</td>
        <td><a class="btn btn-primary" href="{{action('surveyController@update',$ans->id)}}">Vote</a></td>
    </tr>
@endforeach

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Carlos