Laravel 的 get 方法未如預期運行
P粉642436282
P粉642436282 2024-03-27 15:20:23
0
1
390

我正在嘗試使用 get 方法從表單添加數據,但是當我提交它時,數據不會像這樣顯示在 url 中

addcustomers_token=vkmH0qnA6g2mfVcQzYUU3ZE0ZQvkQYs4DuCtKteX&company_name=Hebert Frank Inc&customer_name=Beau Nixon&customer_surname=Schneider&customer Incomer&customer_name=Beau Nixon&customer_surname= Sch phone3 =14871428281&customer_email1=qycuhuva@mailinator.com&customer_score=2&customer_job=Doloribus facilis no&customer_gender=女&customer_source=Tawk Canlı Desuser&customer_gender=tag&customer_source=Tawk

#但是它的顯示是這樣的

/新增客戶

我的控制器

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']);

我的表格

<form action="/add-customers2" method="GET">
                    @csrf
                    <div class="row">
                        <div class="col-md-12 mb-4">
                            <label> <strong> Company Name </strong></label>
                            <input type="text" name="company_name" class="form-control" placeholder="Company Name">
                        </div>

                        <div class="col-md-6 mb-4">
                            <label> <strong> Customer Name </strong></label>
                            <input type="text" name="customer_name" class="form-control" placeholder="Customer Name">
                        </div>
                        <div class="col-md-6 mb-4">
                            <label> <strong> Customer Surname </strong></label>
                            <input type="text" name="customer_surname" class="form-control"
                                placeholder="Customer Surname">
                        </div>





                        <div class="col-md-4 mb-4">
                            <label> <strong>Phone 1</strong> *</label>
                            <input type="text" maxlength="11"
                                oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '');"
                                name="customer_phone1" class="form-control" placeholder="Phone" reqiured>
                        </div>
                        <div class="col-md-4 mb-4">
                            <label> <strong> Phone 2 </strong></label>
                            <input type="text" maxlength="11"
                                oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '');"
                                name="customer_phone2" class="form-control" placeholder="Phone">
                        </div>
                        <div class="col-md-4 mb-4">
                            <label> <strong> Phone 3 </strong></label>
                            <input type="text" maxlength="11"
                                oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '');"
                                name="customer_phone3" class="form-control" placeholder="Phone">
                        </div>







                        <div class="col-md-6 mb-4">
                            <label> <strong> Customer Email </strong></label>
                            <input type="text" name="customer_email1" class="form-control" placeholder="Email">
                        </div>

                        <div class="form-group  col-md-6 mb-4 ">

                            <label class="control-label" for="customer_score"> <strong> Customer Score
                                </strong></label>
                            <select class="form-control " name="customer_score">
                                <option  selected="true" disabled="disabled" hidden>Choose..</option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>


                            </select>


                        </div>
                        <div class="col-md-6 mb-4">
                            <label> <strong> Customer Job </strong></label>
                            <input type="text" name="customer_job" class="form-control" placeholder="Customer Job">
                        </div>
                        <!-- 
                        <div class="col-md-6 mb-4">
                            <label> <strong> Call Later </strong></label>
                            <input type="text" name="call_later" class="form-control" placeholder="Call Later">
                        </div>

                        -->








                        <div class="form-group  col-md-6 mb-4 ">

                            <label class="control-label" for="customer_gender"> <strong> Customer Gender
                                </strong></label>
                            <select class="form-control select2" name="customer_gender">
                                <option value="Man" selected="selected">Man</option>
                                <option value="Woman">Woman</option>

                            </select>


                        </div>











                        <div class="form-group  col-md-12 mb-4 ">

                            <label class="control-label" for="customer_source"> <strong> Customer Source
                                </strong></label>
                            <select class="form-control " name="customer_source">
                                <option selected="true" disabled="disabled" hidden >Choose..</option>
                                <option value="Gelen Arama">Gelen Arama</option>
                                <option value="Tawk Canlı Destek">Tawk Canlı Destek</option>
                                <option value="WhatsApp">WhatsApp</option>
                                <option value="Formlar">Formlar</option>
                                <option value="E-Posta">E-Posta</option>
                                <option value="Referans">Referans</option>



                            </select>


                        </div>
                        <!-- 
                        <div class="col-md-6 mb-4">
                            <label> <strong> Customer Ref </strong></label>
                            <input type="text" name="customer_ref" class="form-control" placeholder="City">
                        </div>
                        -->

                        <?php $userId = Auth::id();  ?>
                        <input type="hidden" name="user_id" class="form-control" value="<?php echo $userId; ?>">


                        <input type="hidden" name="user_company_id" class="form-control" value="1">
                        <input type="hidden" name="tag_id" class="form-control" value="1">
                        <input type="hidden" name="company_id" class="form-control" value="1">




















                        <button type="submit" class="btn btn-dark mb-3 ">Kaydet</button>










                    </div>





                </form>

我想將記錄新增到資料庫並重定向回 /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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!