App\Models\Forum\FeatureVote::createNew PHP Method

createNew() public static method

public static createNew ( $params )
    public static function createNew($params)
    {
        $star = new static($params);
        $star->setType();
        if ($star->isValid()) {
            DB::transaction(function () use($star) {
                // So the strings can be used with interpolation
                // instead of concatenation or sprintf.
                $cost = (string) static::COST;
                $increment = (string) $star->voteIncrement();
                return $star->user->update(['osu_featurevotes' => DB::raw("osu_featurevotes - ({$cost})")]) && $star->topic->update(['osu_starpriority' => DB::raw("osu_starpriority + ({$increment})")]) && $star->saveOrFail();
            });
        }
        return $star;
    }

Usage Example

Example #1
0
 public function voteFeature($topicId)
 {
     $star = FeatureVote::createNew(['user_id' => Auth::user()->user_id, 'topic_id' => $topicId]);
     if ($star->getKey() !== null) {
         return ujs_redirect(route('forum.topics.show', $topicId));
     } else {
         return error_popup(implode(' ', $star->validationErrors()->allMessages()));
     }
 }