TaSvet
TaSvet

Reputation: 442

Can't get Session in controller

I want to alert notify after sent message!

I try to get session message after sent in controller but it not working.

here my controller code :

class ContactController extends Controller
{
    public function index()
    {
        //not work
        if (Session::has('message')) {
            return true;
            if (Session::get('message')) {
                emotify('success', 'Success! Your message has been sent. Thank');
            } else {
                emotify('error', 'Oop! Your message can\'t send. Try Again');
            }
        }
        return view("pages.contact-us");
    }

    public function sendMessage(Request $req)
    {
        $item = [
            "name" => $req->name,
            "email" => $req->email,
            "phone" => $req->phone,
            "message" => $req->message,
            "status" => 1,
            "created_at" => Carbon::now(),
        ];
        try {
            Message::insert($item);
            Session::flash("message", true);
        } catch (Exception $error) {
            Session::flash("message", false);
        }
        return redirect()->back(); // return back to page Contact
    }
}

Upvotes: 0

Views: 143

Answers (3)

Viral Ghodadara
Viral Ghodadara

Reputation: 70

maybe you have forgotten to import the facade.

use Illuminate\Support\Facades\Session;

Upvotes: 1

Milad pegah
Milad pegah

Reputation: 349

Use the helpers:

  • for inserting and making seesion :
seesion()->put();
  • for catch and using the session :
session()->get();

Upvotes: 1

Muhammad
Muhammad

Reputation: 99

if you want alert message after submit form or after progress in view you can use on Controller

 return redirect->back->with('session_name','the message here');

and in view on Blade file

@if (session('session_name'))
  <div > 
     {!! session('session_name') !!} 
  </div>
@endif

Upvotes: 1

Related Questions