【问题标题】:laravel 4 uploading an image resource without reuploading the imagelaravel 4 上传图片资源而不重新上传图片
【发布时间】:2013-12-18 13:40:28
【问题描述】:

我有一个图像资源,将图像存储在 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);
    }
}

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 2013-05-14
      • 2014-12-22
      • 1970-01-01
      • 2015-02-19
      • 2015-03-13
      • 2015-06-05
      • 2021-03-20
      • 1970-01-01
      相关资源
      最近更新 更多