Creating a Contact Module

Create a contact table for storage

the following migration will create a contacts table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('contacts', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('company')->nullable();
            $table->string('email');
            $table->string('phone')->nullable();
            $table->string('preferred_contact')->nullable();
            $table->text('body');
            $table->string('response')->default('Unread');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('contacts');
    }
};

run the migration

php artisan migrate

Create your Contact Model based on the table created

place in app\Models dir

namespace App\Models;


use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    use HasFactory;


    protected $fillable = ['name', 'company', 'email', 'phone',  'preferred_contact', 'body', 'response'];

    protected $searchableFields = ['*'];

}

Mailing

If you are mailing notifications

Double check your .env has correct mail settings to allow sending

Add configuration to your app config

 'email_test_mode' => true,
    'test_emails' => array(        
        'test@meccamedialight.com.au',
    ),
    'booking_notifications_email' => ['test@meccamedialight.com.au'],
    'publish_notifications_email' =>  ['test@meccamedialight.com.au'],
    'contact_notifications_email' =>  ['test@meccamedialight.com.au'],
    'expiry_notifications_email' =>  ['test@meccamedialight.com.au'],

Add a Mailable for the mail you wish to send

app/Mail/ContactSubmittedAdmin.php

<?php

namespace App\Mail;

use App\Models\Contact;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class ContactSubmittedAdmin extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     */
    public function __construct(public Contact $contact)
    {
        //
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Contact form submitted '.$this->contact->name,
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            markdown: 'emails.admin.contact-form-submitted',

        );
    }

    /**
     * Get the attachments for the message.
     *
     *  <int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}

Add an Action for when the Contact Form is Submitted

app/Actions/ContactFormSubmitted.php

<?php

namespace App\Actions;


use App\Mail\ContactSubmittedAdmin;
use App\Models\Contact;
use Exception;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;


class ContactFormSubmitted
{
    private $contact;

    public function __construct()
    {
//        $this->memberRules = $memberRules;
    }



    public function contacted(Contact $contact)
    {
       //Notify admin
        $testMode = Config::get('textstemapp.email_test_mode');
        $allowedTestEmails = Config::get('textstemapp.test_emails');
        $notifyEmail = Config::get('textstemapp.contact_notifications_email');
        foreach ($notifyEmail as $admin) {
            try {
                if(!$testMode){
                    $ok = Mail::to($admin)->send(new ContactSubmittedAdmin($contact));
                }
                else if (in_array($admin, $allowedTestEmails, true)){
                    Mail::to($admin)->send(new ContactSubmittedAdmin($contact));
                }
            } catch (Exception $e){
                Log::error("Failed to send contact notify email ". $e->getMessage());
            }
        }
        return [];
    }
}

Add the email template

resources/views/emails/admin/contact-form-submitted.blade.php

<x-mail::message>
 <h2>There has been a contact form submission</h2>
 <x-mail::table>
  |                         |                            |
  |:----------------------- |:---------------------------|
  | Request created | {{$contact->created_at}}    |
  | Customer Name  | {{$contact->name}}     |
  | Company?  | {{$contact->company}}     |
  | email  | {{$contact->email}}     |
  | Comment  | {{$contact->body}}     |
</x-mail::table>
Thanks,<br>
{{ config('app.name') }}
</x-mail::message>

Wrangler Component

Wrangler Active Component

app\Wrangler\Components\Contact.php

<?php

namespace App\Wrangler\Components;

use Illuminate\Http\Request;
use Medialight\Textstem\Helpers\DataHelper;
use Medialight\Textstem\Models\Contact as ContactModel;
use Medialight\Textstem\Models\WranglerPage;
use Medialight\Textstem\Wrangler\ActiveComponent;

class Contact extends ActiveComponent
{
    public $config;

    //    public static string $description="Displays a list of news items, or a single view if a slug can be identified in the url.";

    public array $editSetup = [
        [
            'controltype' => 'instructions',
            'value' => 'Displays a contact form',
        ],
        [
            'name' => 'title',
            'controltype' => 'text',
            'value' => '',
            'label' => 'Title',
            'instructions' => 'Optional title. Note - this text will be escaped. To enter raw HTML, use the HTML block.',
        ],

        [
            'name' => 'viewtemplate',
            'controltype' => 'select',
            'value' => 'default',
            'label' => 'View template',
            'options' => ['default'],
            'size' => 1,
        ]

    ];

    public function render(Request $request, WranglerPage $wranglerPage)
    {

        $view = $this->getView();
        return view((string)$view, $this->config)->render();
    }



    public function preview()
    {
        $view = $this->getView();

        return view("$view")->render();
    }


}

Livewire

app\Livewire\Forms ContactForm.php

app\Livewire Contact.php