【发布时间】:2015-12-29 10:00:36
【问题描述】:
我有两个表 Projects 和 Galleries。项目有一个画廊。在画廊表中,我有一个外键 project_id。
Schema::table('galleries', function (Blueprint $table) {
$table->integer("project_id")->unsigned()->nullable()->default(null);
$table->foreign("project_id")
->references("id")
->on("projects")
->onDelete("set null");
});
在项目模型中,我有一个函数可以获取与项目关联的图库:
public function gallery()
{
return $this->hasOne("App\Gallery");
}
创建新项目时,用户从图库下拉列表中选择,然后在保存新项目时,如何更新图库表以使用新创建的 projects_id
$gallery_id = 2; // user selected gallery 2
$project = new Project();
$project->title = "new project";
$project->save();
【问题讨论】:
标签: php laravel laravel-5 eloquent