簡體   English   中英

即使已設置可填充數據也不會保存到數據庫中

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM