0
votes

I am trying to insert data in table using laravel and mysql but I see the error SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'univ' cannot be null , while I am not inserting any null value. I am filling all the field in form. what is the reason? please help.

My form is education.blade.php and its code is given.

@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

The code of my migration is given here.

<?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');
    }
}

The code of my controller is given 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)
    {
        //
    }
}

and The code of my model is given here.

    <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

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

and the error which i am getting while submitting the form is given.

Integrity constraint violation: 1048 Column 'univ' cannot be null

3
stop making univ being 'null' - danblack
I am inserting value in univ column and it is not null in database, but same error - Irfan Khan
in store function firstly print this dd($request->all()). Check if value print. - Lakhwinder Singh

3 Answers

2
votes

Your problem lies in the univ column being null. When you wrote the initial migration for that table you didn't add the ->nullable() column modifier. So the SQL that creates the table, which is generated by the ORM, does not have the NULL modifier you'd expect. Two options here depending on your acceptance criteria: 1. Add the nullable modifier with a migration 2. Make sure you submit univ every time you post, and thus make it a required field on the frontend for a good UX.

That list isn't exhaustive, but it'll get you going. Hope all goes well!

1
votes

You need to make the univ column nullable as m.a.solano93 said. Easiest way to do this is below.

php artisan make:migration update_educs_table

In the migrations up method

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

Then run your migration

php artisan migrate
-2
votes

Im not sure what exactly what educ::create(Request::all()); does, but could you try and change that line with the lines below and tell us do you still get the same error. Also a note, you still need to add some text to univ even when you are not sending it, because if you are not sending it, it is treated as null

$education = new educ($request->all());
$education->save();