Skip to content
Advertisement

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
    }
}

Advertisement

Answer

Use the helpers:

  • for inserting and making seesion :
seesion()->put();
  • for catch and using the session :
session()->get();
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement