【发布时间】:2019-02-05 10:27:18
【问题描述】:
我在控制器中有以下方法
public function store(Request $request)
{
$site = Site::create([
"path" => $request->path,
"site_link" => $request->link,
]);
if ($request->features) {
$features = explode(',', $request->features);
foreach ($features as $feature) {
$site->features()->save(SiteFeature::create(["feature" => $feature]));
}
}
return response()->json($site, 201);
}
站点模型有这个方法
public function features()
{
return $this->hasMany('App\SiteFeature');
}
这是我 $fillable 的 SiteFeature 属性
protected $fillable = ['feature', 'site_id'];
由于某种原因,我得到下一个错误
local.ERROR: SQLSTATE[HY000]: 一般错误: 1364 Field 'site_id' 没有默认值(SQL:插入
site_features(feature) 值 (fes)) {"exception":"[object] (照亮\数据库\查询异常(代码:HY000):SQLSTATE[HY000]: 一般错误:1364 字段 'site_id' 没有默认值(SQL: 插入site_features(feature)值(fes))
【问题讨论】:
-
您正在添加一行,但 site_id 没有要插入的值,在数据库架构中也没有默认值,因此 INSERT 失败。
-
你不能放两个模型吗?
-
您正在使用此
protected $fillable = ['feature', 'site_id'];,因此您需要为site_id提供值,否则请更改表格并为列site_id添加默认值
标签: php mysql laravel dingo-api