App\Models\Forum\TopicCover::upload PHP Method

upload() public static method

public static upload ( $filePath, $user, $topic = null )
    public static function upload($filePath, $user, $topic = null)
    {
        $cover = new static();
        DB::transaction(function () use($cover, $filePath, $user, $topic) {
            $cover->save();
            // get id
            $cover->user()->associate($user);
            $cover->topic()->associate($topic);
            $cover->storeFile($filePath);
            $cover->save();
        });
        return $cover;
    }

Usage Example

 public function store()
 {
     if (Request::hasFile('cover_file') !== true) {
         abort(422);
     }
     $topic = null;
     if (presence(Request::input('topic_id')) !== null) {
         $topic = Topic::findOrFail(Request::input('topic_id'));
         priv_check('ForumTopicEdit', $topic)->ensureCan();
         if ($topic->cover !== null) {
             abort(422);
         }
     }
     try {
         $cover = TopicCover::upload(Request::file('cover_file')->getRealPath(), Auth::user(), $topic);
     } catch (ImageProcessorException $e) {
         return error_popup($e->getMessage());
     }
     return json_item($cover, new TopicCoverTransformer());
 }
All Usage Examples Of App\Models\Forum\TopicCover::upload