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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37155541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:46:15  来源:igfitidea点击:

Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given

javascriptphplaravel

提问by Arthur Tarasov

I am trying to build a tagging system using taggingJSon the frontend and laravel-taggingon the backend. I got them both working separately, but I cannot get the tags generated by taggingJS to enter the database.

我正在尝试在前端使用taggingJS并在后端使用laravel-tagging来构建标记系统。我让他们都分开工作,但我无法让 taggingJS 生成的标签进入数据库。

My Laravel controller looks like this:

我的 Laravel 控制器如下所示:

public function storeData(Request $request)
{
    $link = new Links;
    $link->tag_name = $request->taggone;
    $link->save();

    $link->tag(explode(',', $request->taggone));
    $link->save();

    return view('pages.postdata', compact('tags'));
}

The HTML code looks like this:

HTML 代码如下所示:

<div class="example-wrapper">
    <div class="tags well">
        <label for="tag" class="control-label">Tag</label>
        <div data-tags-input-name="taggone" id="tag"></div>
    </div>
</div>

I left taggingJS' Javascript unchanged like this (I am not sure if it is even relevant to this issue):

我让 taggingJS 的 Javascript 像这样保持不变(我不确定它是否与这个问题相关):

<script>
// jQuery on Ready example
(function( $, window, document, undefined ) {
    $( document ).ready(function() {
        var t = $( "#tag" ).tagging();
        t[0].addClass( "form-control" );
        // console.log( t[0] );
    });
})( window.jQuery, window, document );
</script>

If I understand it correctly, the tags input into the field taggoneare supposed to be requested by the controller and saved into the database. But when I submit the form I get the error:

如果我理解正确,输入到字段taggone中的标签应该是控制器请求并保存到数据库中的。但是当我提交表单时,我收到错误:

Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in /home/vagrant/Code/Laravel/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 678

类型错误:传递给 Illuminate\Database\Grammar::parameterize() 的参数 1 必须是数组类型,给定的字符串,在 /home/vagrant/Code/Laravel/vendor/laravel/framework/src/Illuminate/Database/ 中调用第 678 行的 Query/Grammars/Grammar.php

Can I have an advice on how to get it to work?

我可以就如何让它发挥作用提出建议吗?

回答by Noddy

It looks like you're passing a string (instead of an array) from your form. Try to adjust the htmlby adding []to the taggoneinput like this:

看起来您正在从表单中传递一个字符串(而不是数组)。尝试html通过将[]添加到标签输入来调整,如下所示:

<div data-tags-input-name="taggone[]" id="tag"></div>

If you're still receiving the same error, post the complete error message somewhere.

如果您仍然收到相同的错误,请在某处发布完整的错误消息。

回答by Parvez Rahaman

You can do this way

你可以这样做

public function storeData(Request $request)
{
    $link = new Links;
    $link->tag_name = $request->taggone;
    $link1 = $link->save();  //Store it to a variable

    //If your $request->taggone already an array you don't have to explode it, directly use $link1->tag($request->taggone);
    // Explode if it's a string and use $link1->tag(explode(',', $request->taggone));

    $link1->tag($request->taggone); //In case $request->taggone array

    //OR

    $link1->tag(explode(',', $request->taggone)); //In case $request->taggone string

    $link1->save();

   return view('pages.postdata', compact('tags'));
  }