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([
            'title' => 'required|unique:posts',
            'content' => 'required',
            'category_id' => 'nullable|exists:categories,id',
            'tags' => 'nullable|exists:tags,id'
        ]);
        $data = $request->all();
        $post = new Post();
        $data['slug'] = Str::slug($data['title'], '-');
        $post->fill($data);
        $post->save();
        if(array_key_exists('tags', $data)){
            $post->tags()->attach($data['tags']);
        }
        return redirect()->route('admin.posts.show', $post->id);
    }
public function store ( Request $ request ) { $ request -> validate ( [ 'title' => 'required|unique:posts' , 'content' => 'required' , 'category_id' => 'nullable|exists:categories,id' , 'tags' => 'nullable|exists:tags,id' ] ) ; $ data = $ request -> all ( ) ; $ post = new Post ( ) ; $ data [ 'slug' ] = Str :: slug ( $ data [ 'title' ] , '-' ) ; $ post -> fill ( $ data ) ; $ post -> save ( ) ; if ( array_key_exists ( 'tags' , $ data ) ) { $ post -> tags ( ) -> attach ( $ data [ 'tags' ] ) ; } return redirect ( ) -> route ( 'admin.posts.show' , $ post -> id ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// VALIDATION //salvare relazioni nella tabella pivot