Example #1
0
 function syncTags($tags)
 {
     if (!$this->attributes) {
         return false;
     }
     ////////////////////////////////
     // Add address to this object //
     ////////////////////////////////
     foreach ($tags as $k => $tag) {
         if ($tag instanceof Tag) {
             //////////////////////////////////
             // Check if already in database //
             //////////////////////////////////
             $tmp_tag = Tag::type($tag->type)->name($tag->tag)->first();
             if (!$tmp_tag) {
                 //////////////
                 // Validate //
                 //////////////
                 $observer = new \App\Models\TagObserver();
                 $observer->saving($tag);
                 if ($observer->saving($tag) === false) {
                     unset($tags[$k]);
                 }
             } else {
                 $tags[$k] = $tmp_tag;
             }
         }
     }
     ////////////////////////////
     // Delete current address //
     ////////////////////////////
     foreach ($this->tags as $tag) {
         $this->tags()->dissociate($tag);
     }
     ///////////////////////////
     // Associate all address //
     ///////////////////////////
     foreach ($tags as $tag) {
         $this->tags()->associate($tag);
     }
     return true;
 }