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

upload() public static method

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

Usage Example

示例#1
0
 public function store()
 {
     if (Request::hasFile('cover_file') !== true) {
         abort(422);
     }
     $forum = Forum::findOrFail(Request::input('forum_id'));
     if ($forum->cover !== null) {
         abort(422);
     }
     try {
         $cover = ForumCover::upload(Request::file('cover_file')->getRealPath(), Auth::user(), $forum);
     } catch (ImageProcessorException $e) {
         return error_popup($e->getMessage());
     }
     return fractal_item_array($cover, new ForumCoverTransformer());
 }