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

16,500

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.

Share:
16,500
M dunbavan
Author by

M dunbavan

I am a self-employed web developer living and working in London.

Updated on November 25, 2022

Comments

  • M dunbavan
    M dunbavan over 1 year

    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.

    • TunaMaxx
      TunaMaxx about 10 years
      Have you tried a dd() on the "multidimensional array that needs flattening" to see what is there? Sounds like your solution is in that array somewhere.
  • TikaL13
    TikaL13 almost 9 years
    How would that look and where does this take place?
  • Elias Escalante
    Elias Escalante over 8 years
    i guess here $user->galleryImages()->save(json_encode($galleryImage));