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