dqt20140129 2018-10-21 19:32
浏览 118

无法保存在数据库中! ErrorException:preg_replace():参数不匹配,pattern是一个字符串,而replacement是一个数组

I have a problem. I can't solve it. Sorry for my english, I'm French. I have a form and I can not save the captured content. My form view:

<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>

When I validate, I get the error: ErrorException: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

My action controller:

<?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
    }
?>

A example of the result:

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
)

</div>
  • 写回答

2条回答 默认 最新

  • douzhu7507 2018-10-21 19:43
    关注

    I understand the "Newsletter" model has relation with "Post" model. And you want to update the Newsletter's posts?

    If it's true so the right way doing to this is:

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

    And in your Newsletter model must have this relation:

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

    This solves your problem if you want to update your Newsletter's Posts relation.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看