src/Http/Controllers/PostController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(PostFormRequest $request)
    {
        $validatedData = $request->validated() + ['user_id' => auth()->id()];
        try {
            DB::transaction(function () use ($validatedData, $request) {
                if ($request->hasFile('picture')) {
                    $file = $request->file('picture');
                    $path_url = $file->storePublicly('cms', 'public');
                    $validatedData['picture'] = $path_url;
                }
                $post = $this->postStore(request('postType'), $validatedData);
                if ($request->tags != '') {
                    $tags = explode(',', $request->tags);
                    foreach ($tags as $value) {
                        $tag = CmsTag::firstOrCreate(['name' => $value]);
                        $post->cms_tags()->attach($tag);
                    }
                }
                if ($request->hasFile('picture')) {
                    if ($post->getFirstMedia(Post::MEDIA_COLLECTION_NAME)) {
                        $post->clearMediaCollection(Post::MEDIA_COLLECTION_NAME);
                    }
                    $post->addMedia($request->picture)
                        ->withResponsiveImages()
                        ->toMediaCollection(Post::MEDIA_COLLECTION_NAME);
                }
            });
            return redirect()->route('posts.index', ['type' => request('postType')])->with('status', 'post created successfully');
        } catch (\Throwable $th) {
            return redirect()->back()->with('error', 'Something went wrong ' . $th->getMessage());
        }
    }
public function store ( PostFormRequest $ request ) { $ validatedData = $ request -> validated ( ) + [ 'user_id' => auth ( ) -> id ( ) ] ; try { DB :: transaction ( function ( ) use ( $ validatedData , $ request ) { if ( $ request -> hasFile ( 'picture' ) ) { $ file = $ request -> file ( 'picture' ) ; $ path_url = $ file -> storePublicly ( 'cms' , 'public' ) ; $ validatedData [ 'picture' ] = $ path_url ; } $ post = $ this -> postStore ( request ( 'postType' ) , $ validatedData ) ; if ( $ request -> tags != '' ) { $ tags = explode ( ',' , $ request -> tags ) ; foreach ( $ tags as $ value ) { $ tag = CmsTag :: firstOrCreate ( [ 'name' => $ value ] ) ; $ post -> cms_tags ( ) -> attach ( $ tag ) ; } } if ( $ request -> hasFile ( 'picture' ) ) { if ( $ post -> getFirstMedia ( Post :: MEDIA_COLLECTION_NAME ) ) { $ post -> clearMediaCollection ( Post :: MEDIA_COLLECTION_NAME ) ; } $ post -> addMedia ( $ request -> picture ) -> withResponsiveImages ( ) -> toMediaCollection ( Post :: MEDIA_COLLECTION_NAME ) ; } } ) ; return redirect ( ) -> route ( 'posts.index' , [ 'type' => request ( 'postType' ) ] ) -> with ( 'status' , 'post created successfully' ) ; } catch ( \ Throwable $ th ) { return redirect ( ) -> back ( ) -> with ( 'error' , 'Something went wrong ' . $ th -> getMessage ( ) ) ; } }
Store a newly created resource in storage.
Store a newly created resource in storage .
// if media has already a logo then delete previous one and upload new one