public function save_post_cal_attach() { $id = Input::get('post_id'); $count = \CalendarModel::where('post_id', $id)->count(); if ($count == 0) { $post = Post::find($id); $calendar = new \CalendarModel(); $calendar->post_id = $id; $calendar->event_date = $post->created_at; $field = \FeedFieldValue::join(\FeedField::getTableName(), \FeedField::getField('id'), '=', \FeedFieldValue::getField('feed_field_id'))->where(['post_id' => $id, \FeedField::getField('fkey') => 'hours'])->first(); if ($field) { $calendar->period = $field->value; } $calendar->save(); $postLangs = \PostLang::where('post_id', $id)->get(); foreach ($postLangs as $postLang) { $calendarLang = new CalendarLangModel(); $calendarLang->calendar_item_id = $calendar->id; $calendarLang->title = $postLang->title; $calendarLang->lang_id = $postLang->lang_id; $calendarLang->save(); } } else { $calendar = CalendarModel::where('post_id', $id)->get(); foreach ($calendar as $item) { CalendarLangModel::where('calendar_item_id', $item->id)->delete(); } CalendarModel::where('post_id', $id)->delete(); } }