简体   繁体   English

在Laravel中提交帖子表格时,为什么会收到preg_replace()错误?

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

I have built a simple application laravel 4. I have scaffolding setup for adding posts which seems to be working fine. 我已经构建了一个简单的应用程序laravel4。我具有用于添加帖子的脚手架设置,这似乎运行良好。 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 我最近在这里看了文档

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

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 here in 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. 多年来,我一直在用砖墙撞墙。

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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM