我犯了这个错误

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

当我try 使用select form在视图中添加array[]时.但当我删除它时,我没有得到任何错误.我只是想在我的 Select 列表中输入一个多值.我需要用foreach吗?

View

<div class = "form-group {{ $errors->has('approver') ? ' has-error' : '' }}">

    <label for = "approver" class = "control-label">Approver:</label>

    <select name = "approver[]" multiple class = "form-control select2-multi">

        @foreach ($approver as $list)
            <option value = "{{ $list->id }}">{{ $list->username }}</option>
        @endforeach

    </select>

    @if ($errors->has('approver'))
        <span class = "help-block">{{ $errors->first('approver') }}</span>
    @endif

</div>

Controller

public function getDocuments()
{
   $approver = DB::table('users')->where('id', '!=', Auth::id())->get();

   return view ('document.create')->with('approver', $approver);
}

public function postDocuments(Request $request)
{

    $this->validate($request,
    [
        'title' => 'required|regex:/(^[A-Za-z0-9 ]+$)+/|max:255',
        'content' => 'required',
        'category' => 'required',
        'recipient' => 'required',
        'approver' => 'required',
    ]);     


    $document = new Document();
    $approve = new Approve();
    $user = Auth::user();
                                //Request in the form
    $document->title = $request->title;
    $document->content = $request->content;
    $document->category_id = $request->category;
    $approve->approver_id = $request->approver;

    $approve->save();

    $document->save();

    $document->sentToApprovers()->sync([$approve->id],false);

}

Update

我死后转储$approver变量,并给出一个值array.

SC

Also die and dump the $request As you can see here I input the id of 4 and 5 in my select list.

SC

推荐答案

好的,您的问题是您试图将一个数组保存为一个单一值,其中您需要遍历批准者.

将控制器逻辑更改为:

foreach ($request->approver as $approver) {
    $approve              = new Approve();
    $approve->approver_id = $approver;
    $approve->save();

    $document->sentToApprovers()->sync([$approve->id],false);
}

Laravel相关问答推荐

为什么Laravel在API请求时返回BadMethodCallException?

在Laravel中URL中添加保护ID

根据路径的开头重定向

如何从laravel中的另一个表中获取用户名

如何从@foreach 循环中捕获所有数据并将其传递给 值? - Laravel

Laravel 有Many Many to Many To One Eloquent

没有 index.php 的路由不起作用

如何修复无效请求(不支持的 SSL 请求)

在 laravel 中更改时区

函数 mcrypt_get_iv_size() 在 Laravel 4 上已弃用

在 Laravel 的 Homestead 中运行 PHPUnit

数组的旧输入?

PHP Laravel:如何获取客户端浏览器/设备?

Laravel 5 Carbon 全局语言环境

Laravel Eloquent模型如何从关系表中获取数据

如何在 Laravel 中使用补丁请求?

如何判断是否在laravel中使用了分页

如何在laravel中 for each 用户生成唯一的随机值并将其添加到数据库中

Laravel 更新后用户模型错误(用户类包含 3 个抽象方法)

你如何在 Laravel 5.3 的新通知服务生成的Electron邮件中添加图像?