在PHP Laravel中提交数据到数据库时出现错误。

huangapple go评论79阅读模式
英文:

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
&lt;?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(&#39;requestform&#39;);
}
public function postdata(Request $req){
// handeling first serction of multi step form 
$firstname = $req -&gt;input(&#39;firstName&#39;); // taking data from the firstname text field on the request form 
$lastname  = $req -&gt;input(&#39;lastName&#39;); // taking data form the lastname text field on the request form
$phone_num = $req -&gt;input(&#39;phone_number&#39;); // ||
$address   = $req -&gt;input(&#39;address&#39;); // ||
$numAdults = $req -&gt;input(&#39;numberofAdults&#39;); //||
$numkids   = $req -&gt;input(&#39;numberofChildren&#39;);
$data = array(&#39;headofhousehold_firstname&#39; =&gt; $firstname, &quot;headofhousehold_lastname&quot; =&gt; $lastname, &quot;phone&quot; =&gt; $phone_num, &quot;address&quot; =&gt; $address, &quot;numofadults&quot; =&gt; $numAdults, &quot;numofkids&quot; =&gt; $numkids);
DB::table(&#39;family&#39;) -&gt; insert($data);
echo&quot;record successfuly inserted&quot;;
return redirect()-&gt;back()-&gt;with(&#39;success&#39;, &#39;Data inserted successfully.&#39;);
}
}

here is my web.php

       &lt;?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(&#39;postdata&#39;,&#39;RequestInsertController@insform&#39;); // this route is used to display 
//the form view when /postdata is accessed 
Route::post(&#39;requestFormData&#39;,&#39;RequestInsertController@postdata&#39;);   
/* Route::get(&#39;requestform&#39;, [RequestInsertController::class, &#39;postdata&#39;]);
Route::post(&#39;requestform&#39;, [RequestInsertController::class, &#39;insform&#39;]); */
Route::get(&#39;requestform&#39;, [RequestInsertController::class, &#39;insform&#39;])-&gt;name(&#39;request.form&#39;);
Route::post(&#39;requestform&#39;, [RequestInsertController::class, &#39;postdata&#39;])- 
&gt;name(&#39;request.form.submit&#39;);

and in my requestform.blade.php

    &lt;form action=&quot;{{url(&#39;requestFormData&#39;)}}&quot; id=&quot;multi-step-form&quot; class=&quot;multi-step-form&quot; method=&quot;POST&quot;&gt;
&lt;div&gt;
@csrf
&lt;section class =&quot;form-section&quot;&gt;
&lt;h4&gt;Client Info&lt;/h4&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-lg-2 mb-4&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label class=&quot;text-label&quot;&gt;First Name*&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;firstName&quot; class=&quot;form-control&quot; placeholder=&quot;FirstName&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-lg-2 mb-4&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label class=&quot;text-label&quot;&gt;Last Name*&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;lastName&quot; class=&quot;form-control&quot; placeholder=&quot;LastName&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-lg-5 mb-4&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label class=&quot;text-label&quot;&gt;Phone Number*&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;phone number&quot; class=&quot;form-control&quot; placeholder=&quot;Phone Number&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-lg-5 mb-4&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label class=&quot;text-label&quot;&gt;Move in date*&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;moveindate&quot; class=&quot;form-control&quot; placeholder=&quot;Move In Date&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-lg-12 mb-4&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label class=&quot;text-label&quot;&gt; Address&lt;/label&gt;
&lt;div class=&quot;input-group&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;inputGroupPrepend2&quot; aria-describedby=&quot;inputGroupPrepend2&quot; placeholder=&quot;address 1&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;div class = &quot;form-navigation mt-3&quot;&gt;
&lt;button type=&quot;button&quot; class =&quot;  previous btn btn-primary float-left&quot;&gt; previous &lt;/button&gt;
&lt;button type=&quot;button&quot; class =&quot; next btn btn-primary float-right&quot;&gt;Next  &lt;/button&gt;
&lt;button type=&quot;submit&quot; class =&quot; btn btn-success float-right&quot;&gt; Submit &lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;

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(&#39;requestFormData&#39;,&#39;RequestInsertController@postdata&#39;);  

The namespace is not defined.

Replace with :

Route::post(&#39;requestFormData&#39;,[RequestInsertController::class, &#39;postdata&#39;])-&gt;name(&#39;postdata&#39;);

This line will work because the RequestInsertController is defined at the top of your route with :

use App\Http\Controllers\RequestInsertController;

huangapple
  • 本文由 发表于 2023年3月4日 03:21:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定