[英]Laravel update() not working in a foreach even though I have declared $fillable
[英]Data are not being saved into database even though fillable have been set
在我的数据库中,我试图将数据保存到表fish中,但是只保存了bear_id,而不是fish_location或fish_type。 有人可以帮忙吗?
信息:一只熊有很多鱼(一对多的关系)
位指示:
public function submit(Request $request)
{
$bear= Session::get('data');
$test = array();
$test['fish_location'] = $request->fish_location;
$test['fish_type'] = $request->fish_type;
$fish = new fish;
$fish = fish::create([$test]);
$fish->bears()->associate($bear);
$fish->save();
return ('thank you');
}
鱼模型:
class fish extends Eloquent
{
protected $fillable = array('fish_type', 'fish_location', 'bear_id');
public function bears() {
return $this->belongsTo('App\bear');
}
鱼台:
Schema::create('fishs', function (Blueprint $table) {
$table->increments('id');
$table->engine = 'InnoDB';
$table->string('fish_location');
$table->string('fish_type');
$table->integer('bear_id');
$table->timestamps();
});
create()
方法需要设置一个数据数组。 您实际传递的是一个包含数据数组的数组。 删除周围的[]
,您应该会很好:
$fish = fish::create($test);
调用$ fish-> save(); 在$ fish-> bears()-> associate($ bear)之前;
$fish->save();
$fish->bears()->associate($bear);
或尝试
$bear= Session::get('data');
$fish = new fish;
$fish->fish_location=$request->fish_location
$fish->fish_type =$request->fish_type;
$fish->save();
$fish->bears()->associate($bear);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.