2013-12-18 149 views
0

我有一個圖片資源,用於將圖片存儲在aws s3上,然後將s3 key和s3 url存儲在數據庫中。在數據庫中還有圖像元數據屬性,如標題,描述等。我希望允許用戶更新這些屬性,但是當前更新表單提交驗證時需要有圖像字段。有沒有辦法解決這個問題?下面是相關的代碼laravel 4上傳圖片資源時無需重新上傳圖片

更新視圖

@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

更新時,您可以調整規則。在你的模型:

/** 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);