Illuminate \ Database \ QueryException SQLSTATE [23000]:违反完整性约束:1048列“ f_name”不能为空

时间:2020-09-07 05:58:16

标签: php laravel e-commerce

我收到此错误。

Illuminate \ Database \ QueryException SQLSTATE [23000]:完整性 约束违反:1048列“ f_name”不能为空(SQL:插入 变成userdetailsidf_namel_namecompany_namemobile_numberemailaddressupdated_atcreated_at) 值(?,?,?,?,?,?,?,2020-09-07 05:25:53,2020-09-07 05:25:53))

这是我的控制器。

CheckoutController.php

   <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Userdetail;


class CheckoutController extends Controller
{
    public function index()
    {
        $userdetail = Userdetail::get();
        return view ('checkout')->with(compact('userdetail'));
      
    }
   
    public function create()
    {
       return view('checkout.create');
    }


   
    public function store(Request $request, Userdetail $userdetail)
    {

        $userdetail-> id = $request-> id;
        $userdetail-> f_name = $request-> f_name;
        $userdetail-> l_name = $request-> l_name;
        $userdetail-> company_name = $request-> company_name;
        $userdetail-> mobile_number = $request-> mobile_number;
        $userdetail-> email = $request-> email;
        $userdetail-> address = $request-> address;
        
        $userdetail->save();
        return redirect()->route('checkout.store');
       
    }

    
    public function show($id)
    {
        //
    }

  
    public function edit($id)
    {
        //
    }

   
    public function update(Request $request, $id)
    {
        //
    }

   
    public function destroy($id)
    {
        //
    }
}

这是我的刀片文件。

<form method="post" action="{{ route('checkout.store') }}" enctype="multipart/form-data">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <div class="pb-7 mb-7">
            <!-- Title -->
            <div class="border-bottom border-color-1 mb-5">
                <h3 class="section-title mb-0 pb-2 font-size-25">Billing details</h3>
            </div>
            <!-- End Title -->

            <!-- Billing Form -->
            <div class="row">
                <div class="col-md-6">
                    <!-- Input -->
                    <div class="js-form-message mb-6">
                        <label class="form-label">
                        Recipient First Name
                            <span class="text-danger">*</span>
                        </label>
                        <input type="text" class="form-control" name="f_name" placeholder="Jack" aria-label="Jack" required="" data-msg="Please enter receipint first name." data-error-class="u-has-error" data-success-class="u-has-success" autocomplete="off">
                    </div>
                    <!-- End Input -->
                </div>

                <div class="col-md-6">
                    <!-- Input -->
                    <div class="js-form-message mb-6">
                        <label class="form-label">
                        Recipient  Last Name
                            <span class="text-danger">*</span>
                        </label>
                        <input type="text" class="form-control" name="l_name" placeholder="Jack" aria-label="Jack" required="" data-msg="Please enter receipint last name." data-error-class="u-has-error" data-success-class="u-has-success" autocomplete="off">
                    </div>
                    <!-- End Input -->
                </div>

                <div class="w-100"></div>

                <div class="col-md-12">
                    <!-- Input -->
                    <div class="js-form-message mb-6">
                        <label class="form-label">
                            Company name (optional)
                        </label>
                        <input type="text" class="form-control" name="company_name" placeholder="Company Name" aria-label="Company Name" data-msg="Please enter a company name." data-error-class="u-has-error" data-success-class="u-has-success">
                    </div>
                    <!-- End Input -->
                </div>

                <div class="col-md-6">
                    <!-- Input -->
                    <div class="js-form-message mb-6">
                        <label class="form-label">
                        Mobile No
                        </label>
                        <input type="text" class="form-control" name="mobile_number" placeholder="+1 (062) 109-9222" aria-label="+1 (062) 109-9222" required="" data-msg="Please enter your mobile number." data-error-class="u-has-error" data-success-class="u-has-success">
                    </div>
                    <!-- End Input -->
                </div>
            
                <div class="col-md-6">
                    <!-- Input -->
                    <div class="js-form-message mb-6">
                        <label class="form-label">
                            Email address
                            <span class="text-danger">*</span>
                        </label>
                        <input type="email" class="form-control" name="email" placeholder="jackwayley@gmail.com" aria-label="jackwayley@gmail.com" required="" data-msg="Please enter a valid email address." data-error-class="u-has-error" data-success-class="u-has-success">
                    </div>
                    <!-- End Input -->
                </div>

                <div class="w-100"></div>

                <div class="col-md-12">
                    <!-- Input -->
                    <div class="js-form-message mb-6">
                        <label class="form-label">
                            Address
                            <span class="text-danger">*</span>
                        </label>
                        <input type="text" class="form-control" name="address" placeholder="91,Galle Road, Colo0mbo 04" aria-label="91,Galle Road, Colo0mbo 04" required="" data-msg="Please enter a valid email address." data-error-class="u-has-error" data-success-class="u-has-success">
                    </div>
                    <!-- End Input -->
                </div>
                                        

                <div class="w-100"></div>

            </div>
              <button type="submit" class="btn btn-primary-dark-w btn-block btn-pill font-size-20 mb-3 py-3">Place order</button>
         
        </div>
</form> 

web.php

Route::resource('/checkout', 'CheckoutController')->middleware('auth');

用户详细信息模型

<?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Userdetail extends Model
    {
        //
    }

这有什么问题?

1 个答案:

答案 0 :(得分:1)

在Laravel中, save()方法是一种雄辩的方法。在雄辩的ORM中, $ fillable 属性是一个数组,其中包含可以使用成批分配填充的所有表字段。批量分配是指向模型发送数组以直接在数据库中创建新记录。

protected $fillable = [
    'f_name', 
    'l_name', 
    'company_name',
    'mobile_number',
    'email',
    'address',
];   

请参阅mass assignment的官方文档