在提交数据到数据库中的php laravel错误

5cg8jx4n  于 2023-03-04  发布在  PHP
关注(0)|答案(1)|浏览(162)

我试图提交数据到数据库使用laravel。我得到了一个错误控制器不存在时,它显然存在。这里是我的代码

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.');
}

}

这里是我的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');

在我的请求表中

<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>

请指导我将数据提交到数据库中。我不知道我做错了什么。我收到以下错误
请求插入控制器不存在

ffscu2ro

ffscu2ro1#

您的错误在此行中:

Route::post('requestFormData','RequestInsertController@postdata');

未定义命名空间。
替换为:

Route::post('requestFormData',[RequestInsertController::class, 'postdata'])->name('postdata');

这一行可以工作,因为RequestInsertController在路由的顶部定义为:

use App\Http\Controllers\RequestInsertController;

相关问题