1

I have 4 fields which are id, name, prenom, date_naissance. When I insert a recording in my phymyadmin, I don't have a problem with the date_naissance.

enter image description here

However, when I try to insert a date_naissance in my insert form, I have an error message.

enter image description here

SQLSTATE[HY000]: General error: 1364 Field 'date_naissance' doesn't have a default value (SQL: insert into eleves (nom, prenom, updated_at, created_at) values (Benoit, Piret, 2019-03-07 22:31:24, 2019-03-07 22:31:24))

Do you see the problem ?

public function create()
{
    return view('admin.eleves.create', compact('eleves'));
}
public function store(Request $request)
{
    $request->validate([
        'nom' => 'required|string',
        'prenom' => 'required|string',
        'date_naissance' => 'required|date'
    ]);
   Eleve::create($request->all());
   return redirect()->route('eleves.index')
                    ->with('success', 'save');
}

My View create

<form class="panel-body" action="{{route('eleves.store')}}" method="POST">
  @csrf
  <fieldset class="form-group">
     <label for="form-group-input-1">Nom</label>
     <input type="text" name="nom" class="form-control" id="form-group-input-1">
  </fieldset>

  <fieldset class="form-group">
     <label for="form-group-input-1">Prénom</label>
     <input type="text" name="prenom" class="form-control" id="form-group-input-1">
  </fieldset>

  <fieldset class="form-group">
     <label for="form-group-input-1">Date naissance</label>
     <input type="date" name="date_naissance" class="form-control" id="form-group-input-1">
  </fieldset>

  <a href="{{route('eleves.index')}}" class="btn btn-primary pull-right">Back</a>
  <button type="submit" class="btn btn-sm btn-primary">Valider</button>
</form>

Model:

class Eleve extends Model
{
    //
    protected  $fillable = ['nom', 'prenom'];
    protected $dates = ['date_naissance'];
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
user11124425
  • 961
  • 1
  • 11
  • 22
  • 1
    can you show the code of your view (form)? Looks like you are not passing a field called `date_naissance` and SQL does not know how to add a default, therefore it gives the error – ArSeN Mar 07 '19 at 22:38
  • Make sure in the table schema you allow nulls for `date_naissance` unless you can confirm it will always have a date. – Daniel G Mar 07 '19 at 22:43
  • Can you print your `$request->all()` and show it? Check you dump result and make sure you allow null value by default for `date_naissance`. – Sundar Ban Mar 07 '19 at 22:44
  • @ArSeN: I have edited my first message... thanks – user11124425 Mar 07 '19 at 22:47
  • 1
    I guess you have a `$fillable` array in `Eleves` Model , and `date_naissance` ain't in there – Shuyi Mar 07 '19 at 22:48
  • @user11124425 well thanks but it does not really show much since there are no input fields (which would get submitted upon form submission) what so ever, can you show the *rendered* form? – ArSeN Mar 07 '19 at 22:49
  • @Daniel G: It must always have a date – user11124425 Mar 07 '19 at 22:55
  • @ArSeN: Indeed, I have edited... – user11124425 Mar 07 '19 at 23:01
  • 1
    @user11124425 as Shuyi already pointed out, if you want to fill it, which you are doing with `Eleve::create($request->all());`, you'll have to add the `date_naissance` key to the `$fillable` property as well – ArSeN Mar 07 '19 at 23:04
  • @SundarBan: I see this, `array:1 [▼ 0 => array:4 [▼ "_token" => "krCfSEdmggz3yKLf1DHNoXLqEGAmSi9r532Gto2z" "nom" => "Leroi" "prenom" => "Mathieu" "date_naissance" => "1990-01-01" ] ]` – user11124425 Mar 07 '19 at 23:05
  • @ ArSeN: thank you Arsen :-) – user11124425 Mar 07 '19 at 23:14

2 Answers2

1

In your Model

class Eleve extends Model
{
    //
    protected  $fillable = ['nom', 'prenom','date_naissance']; // missing date_naissance here.
    protected $dates = ['date_naissance'];
Sundar Ban
  • 589
  • 5
  • 16
1

You will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.

Fillable you specify which fields are mass-assignable in your model, you can do it by adding the special variable $fillable to the model. So in the model you need to add also date_naissance:

class Eleve extends Model {
    protected $fillable = ['nom', 'prenom','date_naissance']; //only the field names inside the array can be mass-assign
    protected $dates = ['date_naissance'];
}

More details: you can easily understand here What does “Mass Assignment” mean in Laravel

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64