app/Http/Controllers/Admin/PostController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Model\Post  $post
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, Post $post)
    {
        $validateData = $request->validate([
            'title' => 'required',
            'content' => 'required',
            'category_id' => 'required|exists:App\Model\Category,id',
            'tags.*' => 'nullable|exists:App\Model\Tag,id',
            'image' => 'nullable|image',
        ]);
        $data = $request->all();
        if (Auth::user()->id != $post->user_id) {
            abort('403');
        }
        $post->title = $data['title'];
        $post->content = $data['content'];
        if ($post->title != $data['title']) {
            $post->title = $data['title'];
            $slug = Str::slug($data['title'], '-');
            $postSlugControl = Post::where('slug', $slug)->first();
            $counter = 0;
            while ($postSlugControl) {
                $newSlug = $slug . '-' . $counter;
                $postSlugControl = Post::where('slug', $newSlug)->first();
                $counter++;
            }
            if ($postSlugControl) {
                $post->slug = $newSlug;
            } else {
                $post->slug = $slug;
            }
        }
        if (!empty($data["image"])) {
            $img_path = Storage::put("uploads", $data["image"]);
            $data["image"] = $img_path;
        }
        $post->image =  $data["image"];
        $post->category_id = $data['category_id'];
        $post->user_id = Auth::user()->id;
        $updated = $post->update();
        if (!$updated) {
            dd('Update failed...');
        }
        if (!empty($data['tags'])) {
            $post->tags()->sync($data['tags']);
        } else {
            $post->tags()->detach();
        }
        return redirect()->route('admin.posts.show', $post->id);
    }
public function update ( Request $ request , Post $ post ) { $ validateData = $ request -> validate ( [ 'title' => 'required' , 'content' => 'required' , 'category_id' => 'required|exists:App\Model\Category,id' , 'tags.*' => 'nullable|exists:App\Model\Tag,id' , 'image' => 'nullable|image' , ] ) ; $ data = $ request -> all ( ) ; if ( Auth :: user ( ) -> id != $ post -> user_id ) { abort ( '403' ) ; } $ post -> title = $ data [ 'title' ] ; $ post -> content = $ data [ 'content' ] ; if ( $ post -> title != $ data [ 'title' ] ) { $ post -> title = $ data [ 'title' ] ; $ slug = Str :: slug ( $ data [ 'title' ] , '-' ) ; $ postSlugControl = Post :: where ( 'slug' , $ slug ) -> first ( ) ; $ counter = 0 ; while ( $ postSlugControl ) { $ newSlug = $ slug . '-' . $ counter ; $ postSlugControl = Post :: where ( 'slug' , $ newSlug ) -> first ( ) ; $ counter ++ ; } if ( $ postSlugControl ) { $ post -> slug = $ newSlug ; } else { $ post -> slug = $ slug ; } } if ( ! empty ( $ data [ " image " ] ) ) { $ img_path = Storage :: put ( " uploads " , $ data [ " image " ] ) ; $ data [ " image " ] = $ img_path ; } $ post -> image = $ data [ " image " ] ; $ post -> category_id = $ data [ 'category_id' ] ; $ post -> user_id = Auth :: user ( ) -> id ; $ updated = $ post -> update ( ) ; if ( ! $ updated ) { dd ( 'Update failed...' ) ; } if ( ! empty ( $ data [ 'tags' ] ) ) { $ post -> tags ( ) -> sync ( $ data [ 'tags' ] ) ; } else { $ post -> tags ( ) -> detach ( ) ; } return redirect ( ) -> route ( 'admin.posts.show' , $ post -> id ) ; }
Update the specified resource in storage.
Update the specified resource in storage .