【问题标题】:Saving Array, from blade to controller Laravel保存数组,从刀片到控制器 Laravel
【发布时间】:2019-09-23 20:34:09
【问题描述】:

我想将所有这些可能是数组的数据保存到我的答案表中。我尝试了很多方法,但似乎找不到正确的解决方案..有人可以帮我谢谢。我不知道如何将 answer(create.blade.php) 中的数据存储到 AnswerController 中的 store 函数上的数据库中,因为我不明白如何使用 $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>

应答控制器

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

【问题讨论】:

  • 你说你不理解“$key =&gt; $value的东西”,你是说你不习惯使用foreach循环还是别的什么?
  • 我知道如何使用foreach来显示数据,但是为了在其中设置一个键,我不知道该怎么做。

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


【解决方案1】:

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

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

请求包含question_id 作为键的答案数组。因此,您可以通过以下方式获得相关答案:

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

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

【讨论】:

  • 非常感谢先生!我可以知道在 $value 属性中,它存储在什么数据中?是问题ID吗?
  • 欢迎您。我不确定$request-&gt;question 会给您发送的一系列问题。但是使用$questions = Input::get('question'); 可以正常工作。它会给你可以迭代的数组,就像你想要的那样。
猜你喜欢
  • 2019-01-12
  • 2019-04-13
  • 2017-12-16
  • 2021-04-17
  • 2017-12-07
  • 2017-09-03
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多