英文:
errors in submitting data into database in php laravel
问题
我正在尝试使用Laravel将数据提交到数据库。我收到了“Controller does not exist”错误,尽管它明显存在。以下是我的代码:
RequestInsertController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Controllers\Controller;
use App\Http\Requests;
class RequestInsertController extends Controller
{
// 以下函数负责渲染表单视图
public function insform(){
return view('requestform');
}
public function postdata(Request $req){
// 处理多步骤表单的第一部分
$firstname = $req->input('firstName'); // 从请求表单的firstname文本字段中获取数据
$lastname = $req->input('lastName'); // 从请求表单的lastname文本字段中获取数据
$phone_num = $req->input('phone_number'); // ||
$address = $req->input('address'); // ||
$numAdults = $req->input('numberofAdults'); //||
$numkids = $req->input('numberofChildren');
$data = array('headofhousehold_firstname' => $firstname, "headofhousehold_lastname" => $lastname, "phone" => $phone_num, "address" => $address, "numofadults" => $numAdults, "numofkids" => $numkids);
DB::table('family')->insert($data);
echo "记录成功插入";
return redirect()->back()->with('success', '数据成功插入。');
}
}
以下是我的web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthManager;
use App\Http\Controllers\orderitemController;
use App\Http\Controllers\LoginController;
use App\Http\Controllers\RequestInsertController;
Route::post('requestFormData','RequestInsertController@postdata');
Route::get('requestform', [RequestInsertController::class, 'insform'])->name('request.form');
Route::post('requestform', [RequestInsertController::class, 'postdata'])->name('request.form.submit');
在我的requestform.blade.php中
<form action="{{url('requestFormData')}}" id="multi-step-form" class="multi-step-form" method="POST">
<div>
@csrf
<section class="form-section">
<h4>客户信息</h4>
<div class="row">
<div class="col-lg-2 mb-4">
<div class="form-group">
<label class="text-label">名字*</label>
<input type="text" name="firstName" class="form-control" placeholder="名字" required>
</div>
</div>
<div class="col-lg-2 mb-4">
<div class="form-group">
<label class="text-label">姓*</label>
<input type="text" name="lastName" class="form-control" placeholder="姓" required>
</div>
</div>
<div class="col-lg-5 mb-4">
<div class="form-group">
<label class="text-label">电话号码*</label>
<input type="text" name="phone_number" class="form-control" placeholder="电话号码" required>
</div>
</div>
<div class="col-lg-5 mb-4">
<div class="form-group">
<label class="text-label">入住日期*</label>
<input type="text" name="moveindate" class="form-control" placeholder="入住日期" required>
</div>
</div>
<div class="col-lg-12 mb-4">
<div class="form-group">
<label class="text-label">地址</label>
<div class="input-group">
<input type="text" class="form-control" id="inputGroupPrepend2" aria-describedby="inputGroupPrepend2" placeholder="地址 1" required>
</div>
</div>
</div>
</div>
</section>
<div class="form-navigation mt-3">
<button type="button" class="previous btn btn-primary float-left">前一页</button>
<button type="button" class="next btn btn-primary float-right">下一页</button>
<button type="submit" class="btn btn-success float-right">提交</button>
</div>
</form>
请指导我如何将数据提交到数据库。我不知道我做错了什么。我收到以下错误:RequestInsertController不存在。
英文:
I'm trying to submit data into the database using laravel. I got an error Controller does not exist when it clearly exist. Here is my code
RequestInsertController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Controllers\Controller;
use App\Http\Requests;
class RequestInsertController extends Controller
{
// the following function is responsible for rendering the form view
public function insform(){
return view('requestform');
}
public function postdata(Request $req){
// handeling first serction of multi step form
$firstname = $req ->input('firstName'); // taking data from the firstname text field on the request form
$lastname = $req ->input('lastName'); // taking data form the lastname text field on the request form
$phone_num = $req ->input('phone_number'); // ||
$address = $req ->input('address'); // ||
$numAdults = $req ->input('numberofAdults'); //||
$numkids = $req ->input('numberofChildren');
$data = array('headofhousehold_firstname' => $firstname, "headofhousehold_lastname" => $lastname, "phone" => $phone_num, "address" => $address, "numofadults" => $numAdults, "numofkids" => $numkids);
DB::table('family') -> insert($data);
echo"record successfuly inserted";
return redirect()->back()->with('success', 'Data inserted successfully.');
}
}
here is my web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthManager;
use App\Http\Controllers\orderitemController;
use App\Http\Controllers\LoginController;
use App\Http\Controllers\RequestInsertController;
//Route::get('postdata','RequestInsertController@insform'); // this route is used to display
//the form view when /postdata is accessed
Route::post('requestFormData','RequestInsertController@postdata');
/* Route::get('requestform', [RequestInsertController::class, 'postdata']);
Route::post('requestform', [RequestInsertController::class, 'insform']); */
Route::get('requestform', [RequestInsertController::class, 'insform'])->name('request.form');
Route::post('requestform', [RequestInsertController::class, 'postdata'])-
>name('request.form.submit');
and in my requestform.blade.php
<form action="{{url('requestFormData')}}" id="multi-step-form" class="multi-step-form" method="POST">
<div>
@csrf
<section class ="form-section">
<h4>Client Info</h4>
<div class="row">
<div class="col-lg-2 mb-4">
<div class="form-group">
<label class="text-label">First Name*</label>
<input type="text" name="firstName" class="form-control" placeholder="FirstName" required>
</div>
</div>
<div class="col-lg-2 mb-4">
<div class="form-group">
<label class="text-label">Last Name*</label>
<input type="text" name="lastName" class="form-control" placeholder="LastName" required>
</div>
</div>
<div class="col-lg-5 mb-4">
<div class="form-group">
<label class="text-label">Phone Number*</label>
<input type="text" name="phone number" class="form-control" placeholder="Phone Number" required>
</div>
</div>
<div class="col-lg-5 mb-4">
<div class="form-group">
<label class="text-label">Move in date*</label>
<input type="text" name="moveindate" class="form-control" placeholder="Move In Date" required>
</div>
</div>
<div class="col-lg-12 mb-4">
<div class="form-group">
<label class="text-label"> Address</label>
<div class="input-group">
<input type="text" class="form-control" id="inputGroupPrepend2" aria-describedby="inputGroupPrepend2" placeholder="address 1" required>
</div>
</div>
</div>
</div>
</div>
</section>
<div class = "form-navigation mt-3">
<button type="button" class =" previous btn btn-primary float-left"> previous </button>
<button type="button" class =" next btn btn-primary float-right">Next </button>
<button type="submit" class =" btn btn-success float-right"> Submit </button>
</div>
</form>
please guide me into submitting data into the database. I don't know what i am doing wrong. I get the following error
>The RequestInsertController does not exist
答案1
得分: 1
你的错误在这一行:
Route::post('requestFormData','RequestInsertController@postdata');
命名空间未定义。
替换为:
Route::post('requestFormData', [RequestInsertController::class, 'postdata'])->name('postdata');
这行将工作,因为在路由的顶部已经定义了RequestInsertController
:
use App\Http\Controllers\RequestInsertController;
英文:
Your error is in this line:
Route::post('requestFormData','RequestInsertController@postdata');
The namespace is not defined.
Replace with :
Route::post('requestFormData',[RequestInsertController::class, 'postdata'])->name('postdata');
This line will work because the RequestInsertController is defined at the top of your route with :
use App\Http\Controllers\RequestInsertController;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论