app/Http/Controllers/Admin/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(Request $request)
    {
        $request->validate($this->validations);
        $data = $request->all();
        $tempSlug = Str::of($data['title'])->slug("-");
        $count = 1;
        while (Post::where('slug', $tempSlug)->first()) {
            $tempSlug = Str::of($data['title'])->slug("-") . '-' . $count;
            $count ++;
        }
        $data['slug'] = $tempSlug;
        if(isset($data['image'])){
            $imgPath = Storage::put('uploads', $data['image']);
            $data['image'] = $imgPath;
        }
        $newPost = Post::create($data);
        $newPost->tags()->sync(isset($data['tags']) ? $data['tags'] : []);
        return redirect()->route('admin.posts.show', $newPost->id);
    }
public function store ( Request $ request ) { $ request -> validate ( $ this -> validations ) ; $ data = $ request -> all ( ) ; $ tempSlug = Str :: of ( $ data [ 'title' ] ) -> slug ( " - " ) ; $ count = 1 ; while ( Post :: where ( 'slug' , $ tempSlug ) -> first ( ) ) { $ tempSlug = Str :: of ( $ data [ 'title' ] ) -> slug ( " - " ) . '-' . $ count ; $ count ++ ; } $ data [ 'slug' ] = $ tempSlug ; if ( isset ( $ data [ 'image' ] ) ) { $ imgPath = Storage :: put ( 'uploads' , $ data [ 'image' ] ) ; $ data [ 'image' ] = $ imgPath ; } $ newPost = Post :: create ( $ data ) ; $ newPost -> tags ( ) -> sync ( isset ( $ data [ 'tags' ] ) ? $ data [ 'tags' ] : [ ] ) ; return redirect ( ) -> route ( 'admin.posts.show' , $ newPost -> id ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// $request->validate([ //     'title' => 'required|string|max:255', //     'content' => 'required|string|max:65535', //     'published' => 'integer|min:0|max:1', // ]);