Irfan Khan
Irfan Khan

Reputation: 117

Redirect after data insertion in laravel with success message

Hello guyzz I want to redirect back to form for new data entry which is actually form.blade.php view. I can see the data inserted successfully but how I can redirect with success message. my code is given.

public function store(Request $request)
{
   $sname = $request->input('sname');
   $fname = $request->input('fname');
   $gradyear = $request->input('gradyear');
   $phone = $request->input('phone');
   $email = $request->input('email');
   $paddress = $request->input('paddress');
   $prog = $request->input('prog');
   $job = $request->input('job');
   $org = $request->input('org');
   $position = $request->input('position');

   $data = array(
       'sname' => $sname,
       "fname" => $fname,
       "gradyear" => $gradyear,
       "phone" => $phone,
       "email" => $email,
       "paddress" => $paddress,
       "prog" => $prog,
       "job" => $job,          
       "org" => $org,
       "position" => $position
   );

   DB::table('tests')->insert($data);
   echo "Data inserted Successfully";
}

Upvotes: 3

Views: 7312

Answers (3)

Stanley
Stanley

Reputation: 499

On the controller after the code

return back()->with('status', 'successfully inserted');

On the form view

 @if(session('status')
    {{ session('status') }}
    @endif

You can format your message in the CSS you choose

Upvotes: 2

Giovanni S
Giovanni S

Reputation: 2110

Assuming the above code is working the way you want, you can redirect with flashed session data:

public function store(Request $request)
{
   $sname = $request->input('sname');
   $fname = $request->input('fname');
   $gradyear = $request->input('gradyear');
   $phone = $request->input('phone');
   $email = $request->input('email');
   $paddress = $request->input('paddress');
   $prog = $request->input('prog');
   $job = $request->input('job');
   $org = $request->input('org');
   $position = $request->input('position');

   $data = array(
       'sname' => $sname,
       "fname" => $fname,
       "gradyear" => $gradyear,
       "phone" => $phone,
       "email" => $email,
       "paddress" => $paddress,
       "prog" => $prog,
       "job" => $job,          
       "org" => $org,
       "position" => $position);

   DB::table('tests')->insert($data);

   return back()->with('status', 'Data inserted Successfully!');

}

And inside your blade file you can render with something like:

@if (session('status'))
    <div class="alert alert-success">
        {{ session('status') }}
    </div>
@endif

https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data

Upvotes: 0

Sand Of Vega
Sand Of Vega

Reputation: 2416

In Controller:

public function store(Request $request)
{
    //
    return back()->with('success', 'Data inserted Successfully');
}

In Blade:

@if(session()->has('success'))
    <div class="alert alert-success">
        {{ session()->get('success') }}
    </div>
@endif

Upvotes: 0

Related Questions