Home > Mobile >  Laravel: How to return view() with ->withInput()
Laravel: How to return view() with ->withInput()

Time:11-24

How am I able to return view('support') with following:

->withInput()
->with('success', 'Found results');

Code

  // if validation do not fail
        if ($validated->fails() === false) {
            //session()->flashInput($request->input());
            // return
            return view('siteadmin.support.support', [
                'articles' => $articles,
                'categories' => $categories,
            ]);
        }

Following code is not working: Code

  // if validation do not fail
        if ($validated->fails() === false) {
            //session()->flashInput($request->input());
            // return
            return view('siteadmin.support.support', [
                'articles' => $articles,
                'categories' => $categories,
            ])
            ->withInput()
            ->with('success', 'Found results');
        }

CodePudding user response:

You can simply do an array_merge()

if ($validated->fails() === false) {
            //session()->flashInput($request->input());
            // return
            return view('siteadmin.support.support', array_merge($request->all(), [
                'articles' => $articles,
                'categories' => $categories,
                'success' => 'Found Results',
            ]));
        }

CodePudding user response:

So you can do this in multiple ways. Through sessions or through the view. Firstly if you have session messages setup you can throw a message like this

$request->session()->flash('success','Found Results'); 
return view('...')->withInput();

Or you can pass the success message through the view like

return view('siteadmin.support.support', compact('articles', 'categories'), [
   'success' => 'Results Found'
])->withInput();

CodePudding user response:

You may use the withInput method provided by the RedirectResponse instance to flash the current request's input data to the session before redirecting the user to a new location. This is typically done if the user has encountered a validation error. Once the input has been flashed to the session, you may easily retrieve it during the next request to repopulate the form:

return back()->withInput();

CodePudding user response:

Store it in a session you can use session() helper or $request->session->put()

// validation success

session([
    'categories' => $categories,
    'articles' => $articles,
]);

return back();

then you can just access the session value in blade using the session() helper.

<div> {{ session('categories') }}</div>
  • Related