dqp4933 2013-12-18 13:40
浏览 18
已采纳

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

I have a resource for images that stores the image on aws s3 and then stores the s3 key and s3 url in the database. Also in the database are the images meta data attributes like title, description etc. I want to allow the user to update these attributes but currently when the update form submits the validation requires that there be an image field. Is there a way to get around this? Below is the relevant code

update view

@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

controller

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';
        }
    }

model

<?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条回答 默认 最新

  • douduan2272 2013-12-18 14:49
    关注

    You can adjust the rules when updating. In your model:

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

    In your controller, when updating:

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

    Then do your validation:

    $validator = Validator::make($input, $rules);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'