【发布时间】:2016-08-17 17:04:31
【问题描述】:
工作模型中的关系
public function account()
{
return $this->belongsTo(EbayAccount::class, 'id', 'account_id');
}
EbayAccount 中的关系
public function jobs()
{
return $this->hasMany(Job::class, 'account_id', 'id');
}
主要代码 //那个工作
$job = new Job();
$job->account_id = $acc->id;
$job->action = 'getOrders';
$job->days = 30;
$job->save();
//那行不通
Job::create([
'account_id' => $acc->id,
'action' => 'getOrders',
'days' => 30
]);
//error 'account_id' 外键约束失败
创建表作业
$table->integer('account_id')->unsigned()->index();
$table->foreign('account_id')->references('id')->on('ebay_accounts')->onDelete('cascade');
写了
protected $fillable = [
'account_id','action', 'priority', 'pagination', 'p2', 'p3', 'days','day_start', 'day_end', 'done'
];
【问题讨论】:
-
给出表的架构!
标签: laravel foreign-keys relationship