2016-04-24 13 views
2

Beziehung in JobmodelleLaravel "Fremdschlüssel versagt"

public function account() 
{ 
    return $this->belongsTo(EbayAccount::class, 'id', 'account_id'); 
} 

Beziehung in EbayAccount

public function jobs() 
{ 
    return $this->hasMany(Job::class, 'account_id', 'id'); 
} 

Hauptcode // dass die Arbeit

 $job = new Job(); 
     $job->account_id = $acc->id; 
     $job->action = 'getOrders'; 
     $job->days = 30; 
     $job->save(); 

// dass nicht funktionieren

 Job::create([ 
     'account_id' => $acc->id, 
     'action' => 'getOrders', 
     'days' => 30 
    ]); 

// Fehler-Einschränkung 'ACCOUNT_ID' Fremdschlüssel nicht

Tabelle Jobs

$table->integer('account_id')->unsigned()->index(); 
$table->foreign('account_id')->references('id')->on('ebay_accounts')->onDelete('cascade'); 

shchema db erstellen enter image description here

writed

protected $fillable = [ 
     'account_id','action', 'priority', 'pagination', 'p2', 'p3', 'days','day_start', 'day_end', 'done' 
    ]; 
+0

Geben Sie das Schema Ihrer Tabellen! – BeS

Antwort

0

Ich denke, das Problem ist, in Ihrem "Job" -Modell. Haben Sie die $fillable in Ihrem Jobmodell festgelegt. Versuchen Sie, ‚ACCOUNT_ID, Aktion, Tage‘ an die $fillable Array wie folgt:

protected $fillable = ['account_id', 'action', 'days']; 

Dies ermöglicht für die Modellmassenzuordnung zu Ihrer Datenbank. https://laravel.com/docs/5.2/eloquent#mass-assignment

+0

alredy writed protected $ filable = [ 'Konto_ID', 'Aktion', 'Priorität', 'Seitennummerierung', 'p2', 'p3', 'Tage', 'Tag_Start', 'Tag_end', 'erledigt' ]; – Vilintritenmert

+0

Haben Sie das gleiche Problem ... Haben Sie es gelöst? – general666

+0

Nein, verwenden Sie einfach $ job = new Job(); $ job-> speichern(); – Vilintritenmert