Skip to content
Advertisement

Laravel 9.x Contact form can’t connect to class

I’m trying to make a contact form. Main idea is user will send data, I will save the data to database, I will get the data from database, I will send the data as an email.

I was following a tutorial -> https://www.positronx.io/laravel-contact-form-example-tutorial/

When I tried to reach classes on my controller it can not find it.

Controller says this

But I have the file here

When I run the code I’m having this -> Cannot instantiate abstract class IlluminateDatabaseEloquentModel

ContactUsFormController.php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use appModelsContact;
use IlluminateDatabaseEloquentModel;
use IlluminateSupportFacadesMail;

class ContactUsFormController extends Controller
{
    //create contact form
    public function createForm(Request $req){
        return view('contact');
    }
    //store contact form data
    public function ContactUsForm(Request $req){
        //form validation 
        $this->validate($req,[
            'name' => 'required',
            'email' => 'required | email',
            'subject' => 'required',
            'message' => 'required'
        ]);
        //store data in database
        Model::create($req->All());
        //send mail to admin
        Mail::send('mail',array(
            'name' => $req->get('name'),
            'email' => $req->get('email'),
            'subject' => $req->get('subject'),
            'message' => $req->get('message')
        ),function($message) use ($req){
            $message->from($req->email);
            $message->to('MyMail@example.com','admin')->subject($req->get('subject'));
        });
        return back()->with('success', 'We have recieved your message');
    }
}

This one making me thinking. In the tutorial he didn’t write a single code into this file but still calling a class from there. appMailscontact.php

<?php
namespace AppMail;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
class contact extends Mailable
{
    use Queueable, SerializesModels;
    public function __construct()
    {
        //
    }
  public function build()
    {
        return $this->view('view.name');
    }
}

appModelsContact.php

namespace AppMail;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Contact extends Model
{
    use HasFactory;
    public $fillable = ['name', 'email', 'subject', 'message'];

}

Do I have a typo? What I am missing? Sorry I’m a beginner

Advertisement

Answer

the issue you call Model::create(); model is just an abstract class, not your concrete model

replace

Model::create($req->All());

with

Contact::create($req->All());

also change namespace AppMail; in your Contact model to namespace AppModels;

to know more information about Mail facade check this

https://laravel.com/docs/9.x/mail#sending-mail

I hope it’s useful

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement