validate($request, [ 'name' => 'required', 'email' => 'required|email' ]); $name = $request['name']; $fname = preg_replace('/ .*$/', '', $name); $lname = preg_match('/. ./', $name) === 1 ? preg_replace('/^[^ ][^ ]* /', '', $name) : ''; $email = $request['email']; if (env('MAILCHIMP_APIKEY', '') != '' && env('MAILCHIMP_LISTID', '') != '') { // Submit the subscription request Newsletter::subscribe($email, [ 'FNAME' => $fname, 'LNAME' => $lname ]); } // Save to the database on success $subscriptions = new Subscriptions; $subscriptions->name = $name; $subscriptions->email = $email; $subscriptions->save(); return 'success'; } }