即使已设置可填充,数据也不会保存到数据库中

Data are not being saved into database even though fillable have been set

在我的数据库中,我试图将我的数据保存到数据库中,table 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');
    }

鱼table:

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();
});

调用 $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);

create() 方法需要设置一组数据。您实际传入的是一个包含数据数组的数组。去掉周围的[]应该就好了:

$fish = fish::create($test);