peanut's avatar

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.

0 likes
4 replies
foxted's avatar

I am not sure about it, but to fire the updating() and updated() events, I think you should use the update() method

peanut's avatar

Well I wanted to stick with the Eloquent way of doing things, but thanks... I need to stick with this format so it's consistent.. and I need the saved event to still fire as well.

foxted's avatar

The saving and saved event should still fire, since the update() method is calling save().

quickliketurtle's avatar

Why not fire the events yourself? Event::fire('PostMedia.saved', [$post]) And/or create your own observer so you are not depending on eloquents default behavior? Model-Observers

Please or to participate in this conversation.