vendredi 30 octobre 2020

Laravel how can I run two redirect with each other simultaneously?

I'm trying to execute two redirect at the same time, but I don't know how to display this correctly in Laravel. Laravel 5.7 is used.

Here is my code:

    public function store(Request $request)
    {
        
         $validatedData = $request->validate([
            'name' => 'min:2|max:50',
            'details' => 'nullable|max:255',
            'image' => 'nullable',
            'tournament_id' => 'nullable',
            
        ]);
        
          $team = Team::create($validatedData);

        return redirect()
            ->route('tournaments.show', $tournament)
            ->with('success', 'Team erstellt');
        
        return redirect()
        ->route('teams.show', $team)
        ->with('success', 'Team erstellt');
        
        $errors = $validator->errors();
        
        $tournament = Tournament::where('id', $team->tournament_id)->first();


return redirect()->back()->with('error' ,'Error Foo');
    }

Update: This was my old code which I changed I refactoring the old code. The code was before whitout a validation and worked.

public function store(Request $request)
    {
        if(Auth::check()){
            $team = Team::create([
                'name' => $request->input('name'),
                'details' => $request->input('details'),
                'image' => $request->input('image'),
                'tournament_id' => $request->input('tournament_id'), 
            ]);
 
                
            if($team){
                
                $tournament = Tournament::where('id', $team->tournament_id)->first();
                
                if($tournament->size > 1){
                    return redirect()->route('tournaments.show', ['tournament'=> $tournament->id])
                ->with('success' , 'Team erstellt');
                }
                
                return redirect()->route('teams.show', ['team'=> $team->id])
                ->with('success' , 'Team erstellt');
            }
 
        }
         
        return back()->withInput()->with('errors', 'Fehler beim erstellen des Teams');
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire