laravel 4上传图像资源而无需重新上载图像

时间:2013-12-18 13:40:29

标签: php laravel laravel-4

我有一个图像资源,用于将图像存储在aws s3上,然后将s3密钥和s3 url存储在数据库中。同样在数据库中的是图像元数据属性,如标题,描述等。我想允许用户更新这些属性,但是当前更新表单提交验证时需要有图像字段。有办法解决这个问题吗?以下是相关代码

更新视图

@extends('layouts.default')

@section('content')
    <h1>Edit Image</h1>

    @if(Auth::check())

        @include('_partials.errors')

            {{ Form::model($image, array('url' => 'images', 'method' => 'post', 'files' => true)) }}

            {{ Form::token() }}
            <p>
                {{ Form::label('caption', 'Title') }}<br />
                {{ Form::text('caption', Input::old('caption')) }}
            </p>
            <p>
                {{ Form::label('altText', 'AltText') }}<br />
                {{ Form::text('altText', Input::old('altText')) }}
            </p>
            <p>
                {{ Form::label('description', 'Description') }}<br />
                {{ Form::textarea('description', Input::old('description')) }}
            </p>
            <p>
                {{ Form::label('image', 'Image File') }}<br />
                {{ Form::file('image') }}
            </p>
            <p>
                {{ Form::submit('Edit') }}
            </p>

            {{ Form::close()}}
    @else

    <p>Please Login To Continue</p>

    @endif

@stop

@section('footer')
@stop

控制器

public function update($id)
    {
        $image = $this->image->find($id);

        if ($id == Auth::user()->id) {

            $input = Input::all();

            $validation = $this->image->validate($input);

            if ($validation->passes()) {

                $this->image->update(array(
                        'caption' => $input['caption'],
                        'altText' => $input['altText'],
                        'description' => $input['description'],
                        ));

                return Redirect::toRoute('images.show')
                    ->with('message', 'Image Updated')
                    ->with('id', $image);
            } else {
                return Redirect::toRoute('images.edit')
                    ->withErrors($validation)
                    ->withInput();
            }

        } else {
            echo 'update failed';
        }
    }

模型

<?php
    use Aws\s3\Exception;

class Image extends BaseModel
{
    protected $guarded = array();

    public static $rules = array('caption' => 'required|max:60',
        'altText' => 'required|max:100',
        'description' => 'max:255',
        'image' => 'required|image|max:100'
        );

    public function user()
    {
        return $this->belongsTo('User');
    }

    //return currently logged in users images

    public static function yourImages()
    {
        return static::where('userId', '=', Auth::user()->id)->paginate();
    }

    //store image in s3

    public function imageToS3($input, $imagefile, $filename, $key)
    {
        $now = date('Y-m-d H:i:s');

        $s3 = AWS::get('s3');
        $bucket = 'trainercompareimages';
        $sourcefile = $imagefile;


             $response = $s3->putObject(array(
                'Bucket' => $bucket,
                'Key' => $key,
                'SourceFile' => $sourcefile,
                'ACL' => 'public-read',
                'Metadata' => array(
                    'created' => $now,
                    'caption' => $input['caption'],
                    'altText' => $input['altText'],
                    'description' => $input['description']
                    )
                ));


            return $response;

            //return 'Upload failed please try again later';

    }

    //delete image from s3

    public function imageDeletes3($imagekeyname)
    {
        $s3 = AWS::get('s3');
        $bucket = 'trainercompareimages';

        $response = $s3->deleteObject(array(
                'Bucket' => $bucket,
                'Key' => $imagekeyname
                ));

        return $response;
    }

    //store image info and link to s3 in db

    public function imageToDb($s3url, $input, $userid, $key)
    {
        $this->create(array(
            'userId' => $userid,
            's3Key' => $key,
            's3Url' => $s3url,
            'caption' => $input['caption'],
            'altText' => $input['altText'],
            'description' => $input['description']
            ));
    }

    //delete image and info from db

    public function imageDeleteDb($id)
    {
        $this->destroy($id);
    }
}

1 个答案:

答案 0 :(得分:1)

您可以在更新时调整规则。在你的模型中:

/** Don't make $rules static **/
public $rules = array('caption' => 'required|max:60',
                      'altText' => 'required|max:100',
                      'description' => 'max:255',
                      'image' => 'required|image|max:100');

在您的控制器中,更新时:

$rules = $this->image->rules;
$rules['image'] = 'image|max:100';

然后进行验证:

$validator = Validator::make($input, $rules);