参数化错误Laravel

时间:2017-04-22 23:54:20

标签: php laravel laravel-4

我目前正在尝试将数据传递到我的问题和答案表中,但我收到此错误

  

类型错误:传递给Illuminate \ Database \ Grammar :: parameterize()的参数1必须是数组类型,给定字符串,在

中调用

在我的控制器中运行此行时似乎正在发生

class question extends Model {

protected $fillable = [
    'questionnaire_id',
    'text'
];


public function questionnaire() {
    return $this->belongsTo('App\questionnaires');
}

public function answer() {
    $this->belongsToMany('App\answer');
 }
}

下面我包括我的模型,视图和控制器:

型号(问题)

class answer extends Model {

protected $fillable = [
    'question_id',
    'answer'
];

public function answer() {
    return $this->belongsTo('App/Question');
 }
}

型号(答案)

public function store(Request $request)
{
    $question = question::create($request->all());
    $answers = answer::create($request->all());
    $answers->question_id = $question->id;
    foreach ($answers as $answer) {
        $answers->answer = $answer;
        $answers->save();
    }

    $question->save();
    $answer->save();


    return view('/create-question', ['questionnaire'=>$question]);
}

QuestionnaireController @存储

@extends('layouts.master')
@section('section-header')
    <h3>Create a Questionnaire</h3>
@endsection
@section('body')
    <script type="text/javascript" src="{{ URL::asset('js/answeraddition.js') }}"></script>
    <h2 class="questionnaire-creation">{{$questionnaire->title}}</h2>
    {!! Form::open(array('action'=>'QuestionnaireController@store', 'id'=>'questionnaire-creation')) !!}
    {!! Form::hidden('questionnaire_id', Auth()->user()->id, ['class'=>'questionnaire-creation hidden', 'placeholder'=>'Enter a description for your questionnaire', 'readonly'])  !!}
    {!! Form::label('text', 'Question', ['class'=>'mandatory questionnaire-creation'])  !!}
    {!! Form::text('text', '', ['class'=>'questionnaire-creation', 'placeholder'=>'Enter a description for your questionnaire']) !!}
    {!! Form::button('Add Answer', ['class'=>'questionnaire-creation', 'id'=>'answerbutton', 'onclick'=>'addAnswer()']) !!}
    {!! Form::label('answer', 'Answers', ['class'=>'questionnaire-creation', 'id'=>'answer'])!!}
    <div id="answercontainer">
        {!! Form::text('answer', '', ['class'=>'questionnaire-creation', 'id'=>'answerbox', 'placeholder'=>'Insert question..', 'name'=>'answer[]'])!!}
        {!! Form::text('answer', '', ['class'=>'questionnaire-creation', 'id'=>'answerbox', 'placeholder'=>'Insert question..', 'name'=>'answer[]'])!!}
        {!! Form::text('answer', '', ['class'=>'questionnaire-creation', 'id'=>'answerbox', 'placeholder'=>'Insert question..', 'name'=>'answer[]'])!!}
        {!! Form::text('answer', '', ['class'=>'questionnaire-creation', 'id'=>'answerbox', 'placeholder'=>'Insert question..', 'name'=>'answer[]'])!!}
        {!! Form::text('answer', '', ['class'=>'questionnaire-creation', 'id'=>'answerbox', 'placeholder'=>'Insert question..', 'name'=>'answer[]'])!!}
    </div>
    {!! Form::submit('Submit and add a new question', ['class'=>'submitbutton questionnaire-creation']) !!}
    {!! Form::submit('Submit and Finish', ['class'=>'submitbutton questionnaire-creation']) !!}
    {{ Form::close() }}
@endsection

创建-question.blade.php

in Grammar.php line 118
at Grammar->parameterize('2017-04-22 23:30:34') in Grammar.php line 731
at Grammar->compileInsert(object(Builder), array('answer' => array('sdfsfd', 'fdfsfd', 'fsdfs', 'sdffd', 'sdfsd'), 'updated_at' => '2017-04-22 23:30:34', 'created_at' => '2017-04-22 23:30:34')) in Grammar.php line 749
at Grammar->compileInsertGetId(object(Builder), array('answer' => array('sdfsfd', 'fdfsfd', 'fsdfs', 'sdffd', 'sdfsd'), 'updated_at' => '2017-04-22 23:30:34', 'created_at' => '2017-04-22 23:30:34'), 'id') in Builder.php line 2103
at Builder->insertGetId(array('answer' => array('sdfsfd', 'fdfsfd', 'fsdfs', 'sdffd', 'sdfsd'), 'updated_at' => '2017-04-22 23:30:34', 'created_at' => '2017-04-22 23:30:34'), 'id')
at call_user_func_array(array(object(Builder), 'insertGetId'), array(array('answer' => array('sdfsfd', 'fdfsfd', 'fsdfs', 'sdffd', 'sdfsd'), 'updated_at' => '2017-04-22 23:30:34', 'created_at' => '2017-04-22 23:30:34'), 'id')) in Builder.php line 1423
at Builder->__call('insertGetId', array(array('answer' => array('sdfsfd', 'fdfsfd', 'fsdfs', 'sdffd', 'sdfsd'), 'updated_at' => '2017-04-22 23:30:34', 'created_at' => '2017-04-22 23:30:34'), 'id')) in Model.php line 1628
at Model->insertAndSetId(object(Builder), array('answer' => array('sdfsfd', 'fdfsfd', 'fsdfs', 'sdffd', 'sdfsd'), 'updated_at' => '2017-04-22 23:30:34', 'created_at' => '2017-04-22 23:30:34')) in Model.php line 1597
at Model->performInsert(object(Builder), array()) in Model.php line 1488
at Model->save() in Model.php line 574
at Model::create(array('_token' => 'xdxpqamcwlhvlgB4dSRXe2fWhFOQi2b23b5DnGgx', 'questionnaire_id' => '1', 'text' => '131321', 'answer' => array('sdfsfd', 'fdfsfd', 'fsdfs', 'sdffd', 'sdfsd'))) in QuestionnaireController.php line 50
at QuestionnaireController->store(object(Request))
at call_user_func_array(array(object(QuestionnaireController), 'store'), array(object(Request))) in Controller.php line 80
at Controller->callAction('store', array(object(Request))) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(QuestionnaireController), object(Route), 'store') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(QuestionnaireController), object(Route), object(Request), 'store') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\QuestionnaireController', 'store') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53
at require_once('/home/rob/Projects/CIS2301/blogadmin/public/index.php') in server.php line 21

此外,我还包括完整的堆栈跟踪,如果有人能告诉我如何修复或甚至指向正确的方向,我将非常感激。

class ViewController: UIViewController {

    func myFunction() {
        ...
    }
}

1 个答案:

答案 0 :(得分:0)

替换 $answers = answer::create($request->all());

$question->answers()->createMany($request->get('answer'));

请注意,您应该将答案的答案重命名为更有意义。