Laravel 6: redirect with variables AND message - not working

443 Views Asked by At


i am trying to redirect using a function in my main controller with both variables and a message.
The message code is fine and is usually displayed by ->with('abo', 'messagetext').
The Problem is that not both types are redirected, only the variables (without the message) :(


The goal is to redirect the following variables: suchString, userId, suche

public function subscribe(Request $request)
    {
        $suchString = $request->input('suchString');
        $userId = $request->input('userId');
        Subscribe::create([
            'abo_string' => $suchString,
            'userid' => $userId
        ]);
            $suche = Document::where( 'title', 'LIKE', '%' . $suchString . '%' )
                                ->orWhere( 'category', 'LIKE', '%' . $suchString . '%' )
                                ->orWhere( 'description', 'LIKE', '%' . $suchString . '%' )
                                ->get();
        $users = User::all();
        return view('pages.search')->with('abo', 'messagetext');
    } 
2

There are 2 best solutions below

7
On BEST ANSWER

You should try the compact() method to redirect your variable.

 session()->flash( 'abo', 'Your message' );
 return view('pages.search', compact('suchString', 'userId', 'suche' ));
1
On

You are returning a view() not a redirect()

And you can also chain with() as much as you like.

    return redirect()->route('pages.search')
        ->with('abo', 'messagetext')
        ->with('suche', $suche)
        ->with('userId', $userId)
        ->with('suchString', $suchString);

Is likely what you want.

These will all be in the session on your next page view.