无法保存在数据库中! ErrorException:preg_replace():参数不匹配,模式是字符串,而替换是数组

时间:2018-10-21 19:32:02

标签: php laravel laravel-5 eloquent

我有问题。我解决不了对不起,我的英语,我是法语。 我有一个表格,我无法保存捕获的内容。 我的表单视图:

<div class="col-lg-12">
{!! Form::open(['route' => ['admin.newsletter.update', $newsletters['id']] ]) !!}
<input name="_method" value="POST" type="hidden">
<?php
    for($i=0; $i<=5; $i++){
        $current = $i+1;
        ?>
        <div class="col-lg-12">
            <div class="col-lg-11">
                <div class="form-group">
<div class="col-lg-2">
    {!! Form::label("newsletterpost[$i]['post_id']", 'Article n°' . $current) !!}
</div>
<div class="col-lg-10">
    <?php if($newsletters['newsletterpost']) { ?>
        <?php $post_id = $newsletters['newsletterpost'][$i]['post_id']; ?>
        {!! Form::select("newsletterpost[$i]['post_id']", $posts, $post_id, ['class' => 'form-control selectoronlect', 'data-id'=> $i]) !!}
        {!! Form::hidden("newsletterpost[$i]['id']", $newsletters['newsletterpost'][$i]['id'])!!}
    <?php } else { ?>
        {!! Form::select("newsletterpost[$i]['post_id']", $posts, '', ['class' => 'form-control selectoronlect', 'data-id'=> $i]) !!}
    <?php }
        ?>
        {!!  Form::hidden("newsletterpost[$i]['newsletter_id']", $newsletters['id']) !!}
</div>
                </div>
            </div>
            <div class="col-lg-1">
                <p style="text-align: center">
<a href="#" class="url_{{ $i }}" target="_blank"><i class="fa fa-file-text-o"></i></a>
                </p>
            </div>
        </div>
        <br /><br /><br />
        <?php
    }
?>
<br /><br /><br />
<div class="col-lg-12">
    <a href="#" class="btn btn-primary">Aperçu de la newsletter</a>
</div>
<br /><br /><br />
<div class="col-lg-12">
    <div class="form-group">
        <div class="col-lg-2">
            {!! Form::label('state', 'Etat de la newsletter') !!}
        </div>
        <div class="col-lg-10">
            <?php
            $state = $newsletters['state'];
            $etat = array(0 => "Brouillon", 1 => "Publier", 2 => "Déjà envoyé");
            ?>
            {!! Form::select('state', $etat, $state, ['class' => 'form-control']) !!}
        </div>
    </div>
    {!!  Form::hidden('id', $newsletters['id']) !!}
    {!! Form::submit('Valider', ['class' => "btn btn-primary"]) !!}
</div>
{!! Form::close() !!}
</div>
当我验证时,出现错误:ErrorException:preg_replace():参数不匹配,模式是字符串,而替换是数组

我的动作控制器:

<?php
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,  $id)
    {
        echo "<pre>";
        print_r($request->all());
        echo "</pre>";
        die();
        //don't save
        Newsletter::where('id', $id)->update($request->all());
        //redirect
    }
?>

结果示例:

Array
(
    [_token] => dTK7cguWBEF06BBC55RWpFwKOi6hYlBEHtuH42Gi
    [_method] => POST
    [newsletterpost] => Array
        (
            [0] => Array
                (
                    ['post_id'] => 30503
                    ['newsletter_id'] => 2
                )

            [1] => Array
                (
                    ['post_id'] => 30504
                    ['newsletter_id'] => 2
                )
            [2] => Array
                (
                    ['post_id'] => 30505
                    ['newsletter_id'] => 2
                )
            [3] => Array
                (
                    ['post_id'] => 30506
                    ['newsletter_id'] => 2
                )
            [4] => Array
                (
                    ['post_id'] => 30507
                    ['newsletter_id'] => 2
                )
            [5] => Array
                (
                    ['post_id'] => 30508
                    ['newsletter_id'] => 2
                )
        )
    [state] => 0
    [id] => 2
)

2 个答案:

答案 0 :(得分:0)

我了解“新闻简报”模型与“邮政”模型有关系。您想更新新闻通讯的帖子吗?

如果是这样,那么正确的做法是:

$newsletter = Newsletter::find($id);
$posts = collect($request->get('newsletterpost'))->pluck('post_id')->toArray();
$newsletter->posts()->sync($posts);

在您的电子报模型中,必须具有以下关系:

public function posts()
{
    return $this->belongsToMany(\App\Post::class);
}

如果您要更新新闻通讯的帖子关系,这可以解决您的问题。

答案 1 :(得分:0)

感谢您的重播。我有同样的错误,但我想我已经找到问题了。当我执行print_r时,我得到一个数组:

    Array
    (
        [0] => Array
            (
                ['post_id'] => 30503
                ['newsletter_id'] => 2
            )

    )

我的问题出在我的查看表中

{!! Form::select("posts[$i]['post_id']", $posts, $post_id, ['class' => 'form-control selectoronlect', 'data-id'=> $i]) !!}

我继续调查。