I am not sure about it, but to fire the updating() and updated() events, I think you should use the update() method
Aug 12, 2014
4
Level 2
Have two model events fire
This is how my current boot method looks:
/****************
* BOOT
*****************/
public static function boot()
{
parent::boot();
Post::saved(function($post)
{
// Upload media
if( Input::get('gallery_exist') == 1 && ! is_null(Input::file('gallery')[0]) )
{
foreach( Input::file('gallery') as $image )
{
$uploader = new Uploader();
$uploader->setDirectory( static::$directory );
if( ! $uploader->uploadImage( $image, array(static::$thumbnailWidth, static::$thumbnailHeight) ) )
{
$post->errors()->add('upload', 'Image upload failed.');
return false;
}
$file = $uploader->getRecentFile();
// Insert image
$media = new PostMedia();
$media->project_post_id = $post->id;
$media->created_by = $post->created_by;
$media->url = $file;
$media->type = 1;
if( ! $media->save() )
{
$post->errors()->add('gallery', 'Your media could not be attached to this post. Please reference the following error: ' . $media->errors()->first() );
return false;
}
}
}
if( Input::get('youtube_exist') == 1 && Input::get('videos')[0] <> '' )
{
foreach( Input::get('videos') as $video )
{
$media = new PostMedia();
$media->project_post_id = $post->id;
$media->created_by = $post->created_by;
$media->url = $video;
$media->type = 2;
if( ! $media->save() )
{
$post->errors()->add('video', 'Your videos could not be attached to this post. Please make sure they are valid before contacting an administrator.' );
return false;
}
}
}
});
Post::created(function($post) {
// Upload product line attachments
if(count( Input::get('product_line_id') ) > 0)
{
foreach( Input::get('product_line_id') as $product )
{
if( ! self::uploadProductLine( $post->id, $product ) )
{
$post->errors()->add('product', 'Products could not be attached to this post. Please contact an administrator' );
return false;
}
}
}
});
Post::updating(function($post) {
dd($post);
if( Input::get('product_exist') == 1 )
{
foreach( Input::get('product_line_id') as $product )
{
if( ! self::uploadProductLine( $post->id, $product ) )
{
$post->errors()->add('product', 'Products could not be attached to this post. Please contact an administrator' );
return false;
}
}
}
});
}
However, updating or updated won't even fire when I pull a post and then save() it. It fires that method is something in the Post model is specifically changed, but it won't fire if nothing is changed which I still want it to because sometimes the user can add more products to the post.. This is all in one form so I rather not separate it since it's pretty strict.
I don't want to put it in the saving() or saved() method either because when the user creates and updates a post, it asks for different information.
Please or to participate in this conversation.