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

瘦欲@ 提交于 2019-12-24 11:54:23

问题


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

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:

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

This has save functions and other functions inside it too.

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

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

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


回答1:


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.

Hope this helps.



来源:https://stackoverflow.com/questions/21486263/why-do-i-get-preg-replace-error-when-submitting-a-post-form-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!