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


我控制器的代码已指定为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.通过迁移添加可为空的修饰符
2.确保每次发布时都提交univ,从而使其成为良好UX的前端必填字段。

这份清单并不详尽,但可以助您一臂之力。希望一切顺利!

关于mysql - SQLSTATE [23000]:违反完整性约束:1048列“univ”不能为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52737888/

10-09 00:58