保存阵列,从刀片到控制器Laravel

时间:2019-05-06 08:14:35

标签: arrays database laravel laravel-5.5 laravel-5.8

我想将所有可能是数组的数据保存到我的答案表中。我已经尝试了很多方法,但是似乎找不到正确的解决方案。有人可以帮助我谢谢您。我不如何将答案(create.blade.php)中的数据存储到AnswerController中存储功能上的数据库中,因为我不了解如何使用$ key => $ value的东西。我仍在学习,因为我真的对Laravel表示了敬意。 Laravel的专业知识可以帮助我或指导我吗?太谢谢你了。

问题模型

<?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');
    }

}

答案模型

<?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');
    } */
}

答案(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();
    }

路线

<?php

Route::resource('feedbacks','FeedbackController');
Route::resource('questions','QuestionController');
Route::resource('answers','AnswerController');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');

1 个答案:

答案 0 :(得分:0)

当您从请求中得到答案时,主要问题在于您的控制器中:

class HomeCoordinator {
  private let presenter: UINavigationController

  init(for presenter: UINavigationController) {
    self.presenter = presenter
  }

  func start() {
    let homeViewModel = HomeViewModel()

    let homeViewController = HomeViewController(viewModel: viewModel)
    homeViewController.bindViewModel()

    self.presenter.pushViewController(homeViewController, animated: false)
  }
}

该请求包含一个以$answer -> answer = $request->input('answer'); 作为键的答案数组。因此,您可以通过以下方式获得相关答案:

question_id

**关键是Laravel使用点表示法来获取数组帖子。