Categories
General Laravel

Quick Form creation Laravel

(1) create model.php + migration file + controller

php artisan make:model --migration --controller ContactForm

(2) Define table

public function up()
{
    Schema::create('contact_forms', function (Blueprint $table) {
        $table->id();
        $table->text('name');
        $table->text('email');
        $table->text('subject');
        $table->longText('Message');
        $table->timestamps();
    });
}

(3) create table in Database

php artisan migrate

(4) Define Route

Route::post('/contactform', 'ContactFormController@store' );

(5) Create Controller

/**
 * Laravel basic verification of the field email
 */
$this->validate($request, [
    'email' => 'required|email',
]);


/**
 * Create new record in teh database
 */
$contactForm = ContactForm::firstOrCreate([
    'email' => request('email'),
    'name' => request('name'),
    'message' => request('message'),
    'subject' => request('subject'),
]);


if (request()->wantsJson()) {
    Mail::to('[email protected]', request('contactform'))
        ->send(new \App\Mail\ContactForm($request));
    return response('Success Form has been Sent', 201);
}

/**
 * Redirect to the successful page creation
 */
return redirect('/')->with('flash', 'Success You subscribed to our Newsletter ');

(6) Create html email template

php artisan make:mail ContactForm --markdown=emails.contactform