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

时间:2014-01-31 17:25:05

标签: php laravel laravel-4 preg-replace

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

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

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

/**
     * 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.');
    }

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

要在后期控制器中使用的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');
    }

}

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

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

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

我的文件要点中的完整堆栈跟踪是here

有人能指出我为什么会发生这种错误。根据我的研究,它创建了一个需要展平的多维数组,但我不确定这是否属实。

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

1 个答案:

答案 0 :(得分:8)

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

希望这有帮助。

相关问题