php - why do I get preg_replace() error when submitting a post form in Laravel? -
i have built simple application laravel 4. have scaffolding setup adding posts seems working fine. have setup stapler , image uploading package. when setup use single image uploads pretty , works charm. looked @ docs here
it states can multiple uploads went doing explained in docs. here coded pages:
post.php model:
<?php class post extends eloquent { use codesleeve\stapler\stapler; protected $guarded = array(); // 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 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') $photo) { $galleryimage = new galleryimage(); $galleryimage->photo = $photo; $user->galleryimages()->save($galleryimage); } return redirect::route('posts.create') ->withinput() ->witherrors($validation) ->with('message', 'there validation errors.'); }
this has save functions , other functions inside too.
galleryimage.php gallery image model use in 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); } // gallery image belongs post. public function post(){ return $this->belongsto('post'); } }
my create.blade.php template post 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 post form single images attached fine , saves db , works treat when save multiple image uploads error:
errorexception preg_replace(): parameter mismatch, pattern string while replacement array
the full stack trace here in gist of files
can point out me why error happens. research creating multidimensional array needs flattening think unsure if true.
i have been banging head against brick wall ages.
problem when submitting multiple images becomes array of pictures instead of single string. trying save array database instead of string expecting. if make photo variable json_encoded array of pictures should able save them.
hope helps.
Comments
Post a Comment