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.
When I run the code I'm having this -> Cannot instantiate abstract class Illuminate\Database\Eloquent\Model
ContactUsFormController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Models\Contact;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Mail;
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('[email protected]','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. app\Mails\contact.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class contact extends Mailable
{
use Queueable, SerializesModels;
public function __construct()
{
//
}
public function build()
{
return $this->view('view.name');
}
}
app\Models\Contact.php
namespace App\Mail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
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
CodePudding user response:
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 App\Mail;
in your Contact model to namespace App\Models;
to know more information about Mail facade check this
https://laravel.com/docs/9.x/mail#sending-mail
I hope it's useful
CodePudding user response:
For achieving this follow the below steps
1: make two routes one for displaying a form of type get
and one for submitting the form of type post
2: connect your route to a controller method in route closure as follow
Route::get('/contact',function(){
return view('contact');
})->name('contact');
3: redirect your form to post route
<form action="{{ route('contact') }}">
<!-- form input goes here -->
</form>
4: catch the form in route
Route::get('/contact',function(){
// I am storing data to variable to be clear for you
$name = request()->name
$lastName = request()->lastName
$email= request()->email
$phone = request()->phone
// save the data to the database then send it to email
});
the process of storing data and email configuration is wast topic please visit larave doc to learn about it more