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

M d*_*van 1 php preg-replace laravel laravel-4

我已经构建了一个简单的应用程序laravel 4.我有脚手架设置添加帖子似乎工作正常.我已经设置了Stapler和图片上传包.当我设置使用单张图片上传时,它非常好,它的魅力.我最近看过这里的文档

它声明你可以进行多次上传,所以我按照文档中的说明去做.这是我的编码页面:

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

PostsController.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.');
    }
Run Code Online (Sandbox Code Playgroud)

这也保存了功能和其他功能.

GalleryImage.php库图像模型在post控制器中使用

<?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');
    }

}
Run Code Online (Sandbox Code Playgroud)

我的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
Run Code Online (Sandbox Code Playgroud)

当我发布单个图像的表单附加其罚款并保存到数据库并且它有效,但当我保存多个图像上传时,我收到此错误:

ErrorException
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
Run Code Online (Sandbox Code Playgroud)

完整的堆栈跟踪在这里是我的文件的主旨

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

我多年来一直在用砖头撞墙.

小智 8

问题是当你提交多个图像时它变成了一个图像数组而不是单个字符串.所以它试图将数组保存到数据库而不是它期望的字符串.如果你这样做你的照片变量是一个json_encoded图片数组,那么你应该能够保存它们.

希望这可以帮助.