Ich habe ein Laravel-Projekt, das eine MySQL-Datenbank mit ein paar Tabellen verwendet. Lassen Sie uns beginnen, indem Sie alle meine Dateien, die verwendet werden. Mein Problem ist, dass wenn ich versuche zu erstellen die ‚Conseillere‘ in die Datenbank, erhalte ich Fehler:Laravel Projekt Speichern von Daten in der Datenbank funktioniert nicht
'SQLSTATE[HY000]: General error: 1364 Field 'date_anniversaire' doesn't have a default value (SQL: insert into `conseilleres`() values())'
plus ich weiß nicht, ob das, was ich wirklich richtig in der Datenbank gespeichert haben. Grundsätzlich hat das consellere Modell nur 2 date, aber auch nom, prenom, adresse und adresse_courriel. Nom und Prenom sind in einer anderen Tabelle namens Personne.Adresse und Ville sind in einer anderen Tabelle namens Adresse.Adresse_courriel ist in einer anderen Tabelle namens Courriel. Conseillere hat all diese Attribute, aber sie sind in anderen Tabellen/Modellen referenziert/enthalten. Das Modell von Conseillere enthält nur date_anniversaire und date_entree column.Conseillere ist eine Personne. Ich werde einige Dateien einschließen, die benötigt werden, aber ich bin nicht sicher, ob es mehr benötigen würde, um mein Problem zu lösen.
Zuerst wird die Usercontroller Funktion, die die Conseillere in der Datenbank speichern:
public function store(){
$input = Input::all();
$unepersonne = Personne::create([]);
$uncourriel = new Courriel;
$adresse = new Adresse;
$conseillere = Conseillere::create([]);
$uncourriel->adresse_courriel = $input['adresse_courriel'];
$unepersonne->nom = $input['nom'];
$unepersonne->prenom = $input['prenom'];
$adresse->adresse = $input['adresse'];
$adresse->ville = $input['ville'];
//the following function are creating drop downs to input a date. for translation purpose, annee=year mois=months and jour=day
$conseillere->date_anniversaire = ConseillereController::construire_date($input['annee_anniversaire']-1, $input['mois_anniversaire']-1, $input['jour_anniversaire']-1);
$conseillere->date_entree = ConseillereController::construire_date($input['annee_entree']-1, $input['mois_entree']-1, $input['jour_entree']-1);
if ($unepersonne->save()){
} else {
return Redirect::back()->withInput()->withErrors($conseillere->validationMessages());
}
}
construire_date Funktion:
private function construire_date($annee, $mois, $jour)
{
if (checkdate($mois, $jour, $annee)) {
$dateTest = new DateTime;
$dateTest->setDate($annee, $mois, $jour);
return $dateTest->format('Y-m-d');
} else {
return "invalide";
}
}
}
Conseillere Modell:
<?php
namespace App\Models;
class Conseillere extends EloquentValidating {
protected $guarded = array('id');
public $timestamps = false;
public function conseillere() {
return $this->belongsTo('App\Models\Conseillere');
}
protected $fillable = [
'nom',
'prenom',
'adresse',
'ville',
'adresse_courriel',
'date_anniversaire',
'date_entree'
];
/**
* Validation
*/
public $validationMessages;
public function validationRules() {
return [
'date_anniversaire' => 'nullable|date',
'date_entree' => 'nullable|date'
];
}
}
Personne Modell:
<?php
namespace App\Models;
class Personne extends EloquentValidating
{
protected $table = 'personnes';
public $timestamps = false;
protected $fillable = [
'nom',
'prenom',
];
public $validationMessages;
public function validationRules() {
return
[
'personne' => 'required',
];
}
public function utilisateurs(){
return $this->belongsToMany('App\Utilisateur');
}
public function adresse() {
return $this->hasMany('App\Models\Adresse');
}
public function carte_credits(){
return $this->belongsToMany('App\Carte_credit');
}
public function courriels(){
return $this->hasMany('App\Models\Courriel');
}
}
Migrationen von Conseillere:
public function up()
{
Schema::create('conseilleres', function (Blueprint $table) {
$table->increments('id');
$table->dateTime('date_anniversaire');
$table->dateTime('date_entree');
$table->integer('personne_id')
->unsigned();
$table->foreign('personne_id')
->references('id')
->on('personnes')
->onDelete('cascade');
});
}
Migrationen von Personne:
public function up()
{
Schema::create('personnes', function (Blueprint $table) {
$table->increments('id');
$table->string('nom');
$table->string('prenom');
$table->timestamps();
});
}
Ive gesucht für eine Weile und kann nicht verstehen, warum die Speicherfunktion nicht funktioniert und nicht in der Datenbank speichern.
Welche Laravel-Version verwenden Sie? – kunal