Thursday, March 30, 2017

make survey at laravel 5.4 and my sql

i need to make survey for my website.ok 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></th>
                    <th>Question 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></td>
                        <td>:</td>
                    </tr>
         @endforeach
                </tbody>
            </table>
            <a class="btn btn-primary" href="">Vote</a>
        </div>

ok.now i put data manually in database and i retrieve.

i wanna when click on my 'Vote' send my Answer Id but instead always send last ID from table !!!!???

what's wrong?

and if anyone has better code for make survey please let me know

thanks in advanced and sorry for my bad language. eng is not my language



via siros

Advertisement