0
votes

I'm getting the following error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'matricule_per' cannot be null (SQL: insert into `permissions` (`matricule_per`, `date_rentree`, `nbr_jour`, `nbr_jour_reste`, `updated_at`, `created_at`) values (?, ?, ?, ?, 2020-03-05 19:56:59, 2020-03-05 19:56:59))

This is my form:

<div class="form-group row">
    <label class="col-form-label col-md-4">Matricule :</label>
    <div class="col-md-8">
        <select name="matricule_per" class="form-control">
            @foreach ($personnes as $p)
                <option value="{{$p->id}}">{{$p->mle}}</option>
            @endforeach
        </select>
    </div>
</div>

This is PermissionController:

public function insertPer(Request $request)
    {
      $perm = new Permission();

      $perm->matricule_per = $request->input('matricule_per');
      $perm->date_rentree = $request->input('date_rentree');
      $perm->nbr_jour = $request->input('nbr_jour');
      $perm->nbr_jour_reste = $request->input('nbr_jour_reste');

      $perm->save();

      return redirect('permission');

    }

    public function indexP()
    {
      $personnes = DB::table('personnes')->select('id','mle')->get();

      return view('frontend.Permission', compact('personnes'));
    }
2
Hello, this means That the column in the database can not be null. Add nullable() to your column in the migrations file. - hmrneves

2 Answers

1
votes

You must add matricule_per to $fillable property in your model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [...,'matricule_per'];
}

Or

You may Allow (by default) NULL values to be inserted into the matricule_per column.

E.g.

Schema::table('permissions', function (Blueprint $table) {
    //...
    $table->string('matricule_per')->nullable();
});
0
votes

In your Model add this on $fillable like other

 protected $fillable = ['matricule_per'];


or just add this

 protected $guared = [];

For details just check this https://laravel.com/docs/5.7/eloquent#mass-assignment

If you don't want to fill this or sometimes it can e null then you should make it nullable in your migration file

$table->string('matricule_per')->nullale(); 

You should write it y adding a new migration or the same migration.if you change on the previous migration you should give

php artisan migrate:refresh

if you add new migration just

php artisan migrate