简体   繁体   English

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

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

in my database I am trying to save my data into database, table fish, but only my bear_id is being saved and not the fish_location or fish_type. 在我的数据库中,我试图将数据保存到表fish中,但是只保存了bear_id,而不是fish_location或fish_type。 Can anyone help? 有人可以帮忙吗?

Information: one bear have many fish (one to many relationship) 信息:一只熊有很多鱼(一对多的关系)

Contoller: 位指示:

 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');

}

fish model: 鱼模型:

class fish extends Eloquent
{
        protected $fillable = array('fish_type', 'fish_location', 'bear_id');


    public function bears() {
        return $this->belongsTo('App\bear');
    }

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

The create() method expects an array of data to set. create()方法需要设置一个数据数组。 What you've actually passed in is an array that contains the array of data. 您实际传递的是一个包含数据数组的数组。 Remove the surrounding [] and you should be good: 删除周围的[] ,您应该会很好:

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

call $fish->save(); 调用$ fish-> save(); before $fish->bears()->associate($bear); 在$ fish-> bears()-> associate($ bear)之前;

$fish->save();    
$fish->bears()->associate($bear);

Or try 或尝试

$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.

相关问题 即使我声明了 $fillable,Laravel update() 也不能在 foreach 中工作 - Laravel update() not working in a foreach even though I have declared $fillable 输入数据后无法重定向到其他页面,即使数据已经记录在数据库中 - Cannot redirect to other page after input the data, even though the data has been recorded in the database 数据未保存在我的数据库中 - Data not being saved in my database 为什么即使我的表单中具有enctype =“ multipart / form-data”,该文件也无法在POST变量中发送 - why is the file not being sent in POST variable even though I have enctype=“multipart/form-data” in the form jQuery验证程序继续,即使未满足所有规则 - jquery validator continues even though all rules have not been met 即使图像已保存到数据库也无法显示(回显)图像 - unable to display(echo) image even image has been saved to database 插入数据库中的数据(即使已经存在) - Data inserted into the database even though it already exists Eclipse PHP Zend:运行旧脚本,即使已对其进行了修改和保存? - Eclipse PHP Zend: running old script even though it has been modified and saved? 数据无法保存到数据库PHP - Data not being able to be saved into database PHP Laravel 插入的数据没有保存到数据库中 - Laravel inserted data is not being saved into database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM