【发布时间】:2014-01-31 17:25:05
【问题描述】:
我已经构建了一个简单的应用程序 laravel 4。我有用于添加帖子的脚手架设置,这似乎工作正常。我已经设置了订书机和图像上传包。当我设置使用单张图片上传时,它非常好,而且很有魅力。我最近看了文档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
谁能告诉我为什么会发生这个错误。根据我的研究,我认为它创建了一个需要展平的多维数组,但我不确定这是否属实。
多年来,我一直在用这个头撞砖墙。
【问题讨论】:
-
你有没有在“需要展平的多维数组”上尝试过 dd() 看看有什么?听起来您的解决方案在某个数组中。
标签: php laravel laravel-4 preg-replace