app/Http/Controllers/Admin/PostController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $data = $request->all();
        $post = Post::find($id);
        if($data['title'] != $post->title){
            $data['slug'] = Str::slug($data['title'], '-');
        }
        $request->validate([
            'title' => [
                'required',
                Rule::unique('posts')->ignore($id),
            ],
            'tags' => 'nullable|exists:tags,id',
            'content' => 'required',
            'category_id' => 'nullable|exists:categories,id',
        ]);
        $post->update($data);
        if(array_key_exists('tags', $data)){
            $post->tags()->sync($data['tags']);
        }else{
            $post->tags()->detach();
        }
        return redirect()->route('admin.posts.show', $post->id);
    }
public function update ( Request $ request , $ id ) { $ data = $ request -> all ( ) ; $ post = Post :: find ( $ id ) ; if ( $ data [ 'title' ] != $ post -> title ) { $ data [ 'slug' ] = Str :: slug ( $ data [ 'title' ] , '-' ) ; } $ request -> validate ( [ 'title' => [ 'required' , Rule :: unique ( 'posts' ) -> ignore ( $ id ) , ] , 'tags' => 'nullable|exists:tags,id' , 'content' => 'required' , 'category_id' => 'nullable|exists:categories,id' , ] ) ; $ post -> update ( $ data ) ; if ( array_key_exists ( 'tags' , $ data ) ) { $ 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 .
// AGGIORNA RELAZIONE TABELLA PIVOT