When I insert data into fields and after clicking the save button this error shows up, I think the error is my web route file I have defined the route in two ways but it didn't work, I used same method to insert it into another table in MySQL and it worked but when I tried to insert data into another table this error shows up, here is my controller file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use Illuminate\Support\Facades\DB;
use App\User;
use App\patient;
class add1 extends Controller
{
function add1(Request $request){
$request->validate(
[
'fname'=> 'required',
'cnic'=> 'required'
]
);
$query=DB::table('patients')->insert([
'fname'=> $request->input('fname'),
'lname'=> $request->input('lname'),
'cnic'=> $request->input('cnic'),
'contactno'=> $request->input('contactno'),
'gender'=> $request->input('gender'),
'age'=> $request->input('age'),
'dob'=> $request->input('dob'),
'city'=> $request->input('city'),
'address'=> $request->input('address'),
'husbandname'=> $request->input('husbandname'),
'fathername'=> $request->input('fathername'),
'bloodgroup'=> $request->input('bloodgroup'),
'primaryphy'=> $request->input('primaryphy'),
'clientname'=> $request->input('clientname'),
'maritalstatus'=> $request->input('maritalstatus'),
'emergencyno'=> $request->input('emergencyno'),
'category'=> $request->input('category'),
]);
if ($query){
return back()->with ('success','Data has been successfuly inserted!');
}
else{
return back()->with ('fail','something went wrong');
}
}
}
Here is my web.php file:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\login;
use App\Http\Controllers\MainController;
use App\Http\Controllers\patient;
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/registration', function () {
return view('registration');
});
Route::post('add1',[patient::class, 'add1']);
This my blade file:
@extends('layouts.theme')
@section('content')
<style type="text/css">
.form-control {
margin-bottom: 10px;
}
label{
font-size: 13px;
font-weight: bold;
}
.disabled{
background-color: #dddddd;
}
select.form-control{
padding: 0;
cursor: pointer;
}
table{
font-size: 13px;
}
.wellcometext{
font-family: sans-serif;
font-size: 28px;
text-align: center;
margin-top: 250px;
color: black;
}
.slogon {
font-size: 18px;
text-align: center;
font-family: sans-serif;
color: black;
}
.wellcometext:hover{
transition: transform .5s;
}
.modal-backdrop {
background-color: black;}
</style>
<div >
<div >
<div style="margin-top: 50px">
<h4>User Mangement</h4>
<hr>
@if(Session::get('success'))
<div >
{{Session::get('success')}}
</div>
@endif
@if(Session::get('fail'))
<div >
{{Session::get('fail')}}
</div>
@endif
<form action="add1" method="post">
@csrf
<div >
<label for="">Name</label>
<input type="text" name="fname" placeholder="Enter name" value="{{old('name')}}">
</div>
<span style="color:red">@error ('name'){{$message}} @enderror</span>
<div >
<label for="">Type</label>
<select name="cnic">
<option value="0">Select </option>
<option value="General">General</option>
<option value="Doctor">Doctor</option>
<option value="Consultant">Consultant</option>
</select>
</div>
<div>
<button type="submit" >save</button>
</div>
</hr>
</div>
</div>
</div>
@endsection
CodePudding user response:
- First of all please compare your
controller fileName
withcontroller className
As you have shown in your controller
snippet, your class has the name add1
, not patient
.
In the above image you have called
App\patient
&App\patient
, make sure your models must exist in theapp
directory because Inlaravel8
models are allocated in theApp\models
directory.In your
web.php
route file. You are callingpatient
instead you have to callcontroller
class which isadd1
.