Laravel 的 get 方法未按预期运行
P粉642436282
P粉642436282 2024-03-27 15:20:23
[PHP讨论组]

我正在尝试使用 get 方法从表单添加数据,但是当我提交它时,数据不会像这样显示在 url 中

addcustomers_token=vkmH0qnA6g2mfVcQzYUU3ZE0ZQvkQYs4DuCtKteX&company_name=Hebert+Frank+Inc&customer_name=Beau+Nixon&customer_surname=Schneider&customer_phone1=19655489736&customer_phone2=16612429699&customer_phone3 =14871428281&customer_email1=qycuhuva%40mailinator.com&customer_score=2&customer_job=Doloribus+facilis+no&customer_gender=女人&customer_source=Tawk+Canlı+Destek&user_id=1&user_company_id=1&tag_id =1&company_id=1

但是它的显示是这样的

/添加客户

我的控制器

public function store(Request $req)
    {
        //
         // Form validation (Zorunlu Alanlar)
        $this->validate(request(), [
            'customer_phone1' => 'required',

     





           
        ]);

     

     
        $customers = new Customers;
       
        $customers->customer_name              = $req->input('customer_name');
        $customers->customer_surname           = $req->input('customer_surname');
        $customers->customer_phone1            = $req->input('customer_phone1');
      
        $customers->customer_phone2            = $req->input('customer_phone2');
      
        $customers->customer_phone3            = $req->input('customer_phone3');
        $customers->customer_email1            = $req->input('customer_email1');
        $customers->customer_score             = $req->input('customer_score');
        $customers->customer_job               = $req->input('customer_job');
        $customers->customer_gender            = $req->input('customer_gender');
        $customers->customer_source            = $req->input('customer_source');
        $customers->customer_ref               = "";
        $customers->company_id                 = $req->input('company_id');




        $customers->user_company_id            = $req->input('user_company_id');
        $customers->user_id                    = $req->input('user_id');
        $customers->tag_id                     = $req->input('tag_id');
        
        if(!empty($req->company_name)){
        $companies = new Companies;
        $companies->company_name              = $req->input('company_name');
        $companies->company_type              = "";
        $companies->company_address           = "";
        $companies->company_country           = "";
        $companies->company_city              = "";
        $companies->company_phone             = "";
        $companies->company_email             = "";
        $companies->company_tax_office        = "";
        $companies->company_tax_no            = "";
        $companies->company_website           = "";
        $companies->company_score             = "";
        $companies->company_status            = "";




        $companies->user_company_id         = "1";
        $companies->user_id                 = $req->input('user_id');
        $companies->tag_id                  = "1";
    

        $companies->updateTimestamps();
        $companies->save();}
     
        $customers->updateTimestamps();
        $customers->save();
 
        return redirect()->back()->with('success', true);
        }

相关路线

Route::get('/addcustomers', function () {
    $companies = App\Models\Companies::all();  
    return view('addcustomers' , compact('companies'));
});

Route::get('/addcustomers2', [App\Http\Controllers\CustomersController::class, 'store']);

我的表格

@csrf

我想将记录添加到数据库并重定向回 /addcustomers url

我做错了什么?

P粉642436282
P粉642436282

全部回复(1)
P粉211273535

您在网址中看到 /addcustomers 的原因很可能是因为提交后您有:

return redirect()->back()->with('success', true);

我假设您的blade/html表单提交页面位于/addcustomers上,在您将其提交到/addcustomers2后,它会重定向回/addcustomers code> 由于上面的代码。您可以通过在浏览器上打开检查器并检查网络选项卡来确认这一点(您可能需要单击一个选项来保留历史记录)。在那里您可能会看到带有所有参数的 /addcustomers2。

正如其他人所说,您应该使用 POST 并重命名您的路线:

  • GET /addcustomers -> GET /customers
  • GET /addcustomers2 -> POST /customer
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号