app/Http/Controllers/TagController.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)
    {
        $validated = $request->validate([
            'tag' => 'required|max:60',
        ]);
        $show = Show::find($request['show_id']);
        $tag = Tag::firstWhere('tag',$request['tag']);
        if(empty($tag)) {
            $tag = new Tag;
            $tag->tag = $request['tag'];
            $result = $tag->save();
            if(!$result) {
                $request->session()->flash('message','Le nouveau mot-clé n\'a pas pu être ajouté !');
                $request->session()->flash('alert-class','alert-danger');
                return redirect()->route('show_show',$show->id);
            }
        }
        if($tag) {
            try {
                $result = DB::table('show_tag')->insert([
                    ['show_id' => $show->id, 'tag_id' => $tag->id],
                ]);
            } catch(\Illuminate\Database\QueryException $qe) {
                $request->session()->flash('message','Une exception s\'est produite ! Le mot-clé n\'a pas pu être associé à ce spectacle !');
                $request->session()->flash('alert-class','alert-danger');
                return redirect()->route('show_show',$show->id);
            }
            if($result) {
                $request->session()->flash('message','Le mot-clé a bien été associé à ce spectacle !');
                $request->session()->flash('alert-class','alert-success');
                return redirect()->route('show_show',$show->id);
            }
        }
    }
public function store ( Request $ request ) { $ validated = $ request -> validate ( [ 'tag' => 'required|max:60' , ] ) ; $ show = Show :: find ( $ request [ 'show_id' ] ) ; $ tag = Tag :: firstWhere ( 'tag' , $ request [ 'tag' ] ) ; if ( empty ( $ tag ) ) { $ tag = new Tag ; $ tag -> tag = $ request [ 'tag' ] ; $ result = $ tag -> save ( ) ; if ( ! $ result ) { $ request -> session ( ) -> flash ( 'message' , 'Le nouveau mot-clé n\'a pas pu être ajouté !'); $ request -> session ( ) -> flash ( 'alert-class' , 'alert-danger' ) ; return redirect ( ) -> route ( 'show_show' , $ show -> id ) ; } } if ( $ tag ) { try { $ result = DB :: table ( 'show_tag' ) -> insert ( [ [ 'show_id' => $ show -> id , 'tag_id' => $ tag -> id ] , ] ) ; } catch ( \ Illuminate \ Database \ QueryException $ qe ) { $ request -> session ( ) -> flash ( 'message' , 'Une exception s\'est produite ! Le mot-clé n\'a pas pu être associé à ce spectacle !'); $ request -> session ( ) -> flash ( 'alert-class' , 'alert-danger' ) ; return redirect ( ) -> route ( 'show_show' , $ show -> id ) ; } if ( $ result ) { $ request -> session ( ) -> flash ( 'message' , 'Le mot-clé a bien été associé à ce spectacle !'); $ request -> session ( ) -> flash ( 'alert-class' , 'alert-success' ) ; return redirect ( ) -> route ( 'show_show' , $ show -> id ) ; } } }
Store a newly created resource in storage.
Store a newly created resource in storage .
//Rechercher le tag //Ajouter à la table tags si nouveau mot-clé //Ajouter à la table show_tag