【问题标题】:why do I get preg_replace() error when submitting a post form in Laravel?为什么在 Laravel 中提交表单时出现 preg_replace() 错误?
【发布时间】: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


【解决方案1】:

问题是当您提交多张图片时,它会变成一组图片而不是单个字符串。所以它试图将数组保存到数据库而不是它期望的字符串。如果你做到了,你的照片变量是一个 json_encoded 图片数组,那么你应该能够保存它们。

希望这会有所帮助。

【讨论】:

  • 那会是什么样子?发生在哪里?
  • 我猜这里是 $user->galleryImages()->save(json_encode($galleryImage));
猜你喜欢
  • 1970-01-01
  • 2020-02-09
  • 1970-01-01
  • 2011-07-28
  • 2014-02-25
  • 2018-11-24
  • 2017-08-24
  • 2011-01-06
相关资源
最近更新 更多