mysql - SQLSTATE[23000] : Integrity constraint violation: 1048 Column 'univ' cannot be null

标签 mysql laravel

我正在尝试使用 laravel 和 mysql 在表中插入数据,但我看到错误 SQLSTATE[23000]: Integrityconstrainviolation: 1048 Column 'univ' can not be null ,而我没有插入任何空值。我正在填写表格中的所有字段。是什么原因?请帮忙。

我的表单是education.blade.php,并给出了它的代码。

@section('content')

    <div class="container"><br>
        <h1 class="text-success text-center">Add Education Here</h1><br>
        <div class="col-md-offset-3 col-md-6 m-auto d-block">
            <form action="edu" method="post">
                <input type="hidden" name="_token" value="{{csrf_token()}}">
                <input type="text" name="degree" value=" MSc / BS" disabled>
                <div>
                    <div class="form-group">
                        <label>University: </label>
                        <input type="text" name="univ" id="" class="form-control">
                    </div>

                    <div class="form-group">
                        <label>Country: </label>
                        <input type="text" name="country" id="" class="form-control">
                    </div>

                    <div class="form-group">
                        <label>Year: </label>
                        <input type="number" name="year" id="" class="form-control">
                    </div>

                    <div class="form-group">
                        <label>Research Area: </label>
                        <input type="text" name="research" id="" class="form-control">
                    </div>
                </div>
                <input type="submit" name="submit" value="ADD!" class="btn btn-lg col-md-offset-3 col-md-6 m-auto d-block">
            </form>
        </div>
    </div>

@endsection

这里给出了我的迁移代码。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEducsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('educs', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('registrations');
            $table->string('degree');
            $table->string('univ');
            $table->string('country');
            $table->integer('year');
            $table->text('research_area');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('educs');
    }
}

我的 Controller 的代码是EduContoller.php

<?php

namespace App\Http\Controllers;

use Request;

use App\educ;

class EduController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('education');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        educ::create(Request::all());
        return 'inserted';
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

这里给出了我的模型的代码。

    <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class educ extends Model
    {
        //
        protected $fillable = ['user_id','degree','univ','country','year','research_area'];
    }

并给出了我在提交表单时遇到的错误。

违反完整性约束:1048 列“univ”不能为空

最佳答案

您的问题在于 univ 列为空。当您为该表编写初始迁移时,您没有添加 ->nullable() 列修饰符。因此,创建表(由 ORM 生成)的 SQL 没有您期望的 NULL 修饰符。这里有两个选项,具体取决于您的接受标准: 1. 通过迁移添加 nullable 修饰符 2. 确保每次发帖时都提交 univ,从而使其成为前端的必填字段,以获得良好的用户体验。

该列表并不详尽,但它可以帮助您继续前进。希望一切顺利!

关于mysql - SQLSTATE[23000] : Integrity constraint violation: 1048 Column 'univ' cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52737888/

相关文章:

laravel - 如何禁用 Stripe 支付中的卡?仅显示 Apple Pay、Google Pay

php - CentOS 7 上的 Laravel 5 无法在域上运行

c# - NHibernate 无法匹配 DateTime 对象

MySQL Update Join 不影响所有匹配的行

php - Laravel 4 命名空间 Controller 中的路由和命名空间

php - 拉维尔 4 : use Facade in custom Class

php - 如何显示数据库中的图像(BLOB 类型数据)?

php - 当我注销时页面未正确重定向

java - 使用不同驱动程序时 PreparedStatement 之间的区别

Laravel 5 无限滚动 + 分页