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

时间:2018-10-10 10:15:48

标签: mysql laravel

我正在尝试使用laravel和mysql在表中插入数据,但是我看到错误SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'univ' cannot be null,而我没有插入任何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');
    }
}

控制器的代码已提供给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”不能为空

3 个答案:

答案 0 :(得分:2)

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

该列表并非详尽无遗,但可以助您一臂之力。希望一切顺利!

答案 1 :(得分:1)

您需要像m.a.solano93所说的那样使univ列为空。 最简单的方法如下。

php artisan make:migration update_educs_table

在迁移up方法中

Schema::table('educs', function (Blueprint $table) {
    $table->string('univ')->nullable()->change();
});

然后运行您的迁移

php artisan migrate

答案 2 :(得分:-2)

我不确定educ::create(Request::all());到底是做什么的,但是您可以尝试更改下面的代码行,并告诉我们您仍然遇到相同的错误吗? 另外请注意,即使不发送文本,您仍然需要向univ添加一些文本,因为如果不发送文本,它将被视为null < / p>

$education = new educ($request->all());
$education->save();
相关问题