为什么在 Laravel 中提交帖子表单时会出现 preg_replace() 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21486263/
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 08:56:50  来源:igfitidea点击:

why do I get preg_replace() error when submitting a post form in Laravel?

phplaravellaravel-4preg-replace

提问by M dunbavan

I have built a simple application laravel 4. I have scaffolding setup for adding posts which seems to be working fine. I have setup Stapler and image uploading package. When I setup to use single image uploads its pretty good and it works a charm. I recently looked at the docs here

我已经构建了一个简单的应用程序 laravel 4。我有脚手架设置来添加似乎工作正常的帖子。我已经设置了订书机和图像上传包。当我设置使用单张图像上传时,它非常好,而且很有魅力。我最近在这里查看了文档

It states that you can do multiple uploads so I went about doing it as explained in the docs. Here are my coded pages:

它指出您可以进行多次上传,因此我按照文档中的说明进行了操作。这是我的编码页面:

Post.php model:

Post.php 模型:

<?php

class Post extends Eloquent {
    use Codesleeve\Stapler\Stapler;
    protected $guarded = array();

    // A user has many profile pictures.
    public function galleryImages(){
        return $this->hasMany('GalleryImage');
    }

    public static $rules = array(
        'title' => 'required',
        'body' => 'required'
    );

    public function __construct(array $attributes = array()) {
    $this->hasAttachedFile('picture', [
        'styles' => [
            'thumbnail' => '100x100',
            'large' => '300x300'
        ],
        // 'url' => '/system/:attachment/:id_partition/:style/:filename',
        'default_url' => '/:attachment/:style/missing.jpg'
    ]);

    parent::__construct($attributes);
    }

}

PostsController.php

帖子控制器.php

/**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $input = Input::all();
        $validation = Validator::make($input, Post::$rules);

        if ($validation->passes())
        {
            $this->post->create($input);

            return Redirect::route('posts.index');
        }
        $post = Post::create(['picture' => Input::file('picture')]);

        foreach(Input::file('photos') as $photo)
        {
            $galleryImage = new GalleryImage();             
            $galleryImage->photo = $photo;                   
            $user->galleryImages()->save($galleryImage);    
        }


        return Redirect::route('posts.create')
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

This has save functions and other functions inside it too.

这里面也有保存功能和其他功能。

GalleryImage.php gallery image model to use in the post controller

GalleryImage.php 画廊图像模型在后期控制器中使用

<?php

class GalleryImage extends Eloquent {
    protected $guarded = array();

    public static $rules = array();

    public function __construct(array $attributes = array()) {

    $this->hasAttachedFile('photo', [
        'styles' => [
            'thumbnail' => '300x300#'
        ]
    ]);

    parent::__construct($attributes);
    }

    // A gallery image belongs to a post.
    public function post(){
        return $this->belongsTo('Post');
    }

}

My create.blade.php template to post the post itself

我的 create.blade.php 模板发布帖子本身

@extends('layouts.scaffold')

@section('main')

<h1>Create Post</h1>

{{ Form::open(array('route' => 'posts.store', 'files' => true)) }}
    <ul>
        <li>
            {{ Form::label('title', 'Title:') }}
            {{ Form::text('title') }}
        </li>

        <li>
            {{ Form::label('body', 'Body:') }}
            {{ Form::textarea('body') }}
        </li>

        <li>
            {{ Form::file('picture') }}
        </li>
        <li>
            {{ Form::file( 'photo[]', ['multiple' => true] ) }}
        </li>

        <li>
            {{ Form::submit('Submit', array('class' => 'btn btn-info')) }}

    </ul>
{{ Form::close() }}

@if ($errors->any())
    <ul>
        {{ implode('', $errors->all('<li class="error">:message</li>')) }}
    </ul>
@endif

@stop

When I post the form with a single images attached its fine and saves to the db and it works a treat but when I save it with multiple image uploads I get this error:

当我发布带有单个图像的表单并保存到数据库时,它可以正常工作,但是当我使用多个图像上传保存它时,我收到此错误:

ErrorException
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

The full stack trace is herein my gist of the files

完整的堆栈跟踪是在这里,我的文件要点

Can anyone point out to me why this error happens. From my research its creating a multidimensional array that needs flattening I think but I am unsure if this is true.

任何人都可以向我指出为什么会发生此错误。根据我的研究,我认为它创建了一个需要展平的多维数组,但我不确定这是否属实。

I have been banging my head against a brick wall with this for ages.

多年来,我一直把头撞在砖墙上。

回答by ProgTimesPie

Problem is when your submitting multiple images it becomes an array of pictures instead of a single string. So its trying to save an array to the database instead of a string which its expecting. If you make it so your photo variable is a json_encoded array of pictures then you should be able to save them.

问题是当您提交多张图片时,它会变成一组图片而不是单个字符串。所以它试图将一个数组保存到数据库而不是它期望的字符串。如果您将照片变量设置为 json_encoded 图片数组,那么您应该能够保存它们。

Hope this helps.

希望这可以帮助。