2017-05-08 31 views
1

我在laravel中有一個用於將數據保存到數據庫中的函數,出於某種原因,其中一個變量將不會添加到數據庫中,特別是日曆的id 。我曾嘗試在數據庫輸入前後返回變量,並且在兩次都存在,它不會被添加。以下是我迄今爲止:傳遞給函數的laravel值不會傳遞到數據庫中

class SetupPhotoshoot{ 

public function createListing(Request $request){ 

$calendarAdd = $this->addToCalendar($request); 

$addtoDB = $this->addToDB($request, $calendarAdd['id']); 

return $addtoDB; 

    } 
protected function addToDB($request, $calendarID){ 

    //Some code here 

    return $calendarID; //This is to see if the id exists inside the function, which it does. 

    $newSchedule = new Schedule; 

    $addtoDB = $newSchedule->create({ 
    //Add a bunch of fields 
    'services_id' => $request->Package, 
    'appt_time' => $request->DateTime, 
    'total' => $request->total, 
    'calendar_id' => $calendarID //This is where I want the id to be added, but it isn't working. 
}); 

    return $calendarID; //I tried returning after adding to DB to see if it somehow got lost between before adding to DB and after, but it still returned. 

//NOTE: the two individual returns were NOT used at the same time. I commented out one to test if the other returned, so it IS NOT because I used return multiple times. 

    } 

} 

所有其他數據被添加到數據庫中正確的,除了calendar_id.它說,特定的錯誤是「常規錯誤:1364字段‘CALENDAR_ID’沒有缺省值「。當我允許空值時,它只會將「null」添加爲數據庫中的值。下面是它看起來像在數據庫中(這是MYSQL):

Name: calendar_id 

Type: varchar(191) utf8mb4_unicode_ci 

Null: Yes 

Default: None 
+0

怎樣的schedule.create()是什麼樣子? – Shadow

回答

0

'calendar_id' doesn't have a default value

$calendarID沒有被通過,也許這是空,用dd($calendarID)以確保它被設置。

create()也是一個靜態方法,所以不要創建的Schedule 一個實例,你可以靜態Schedule::create($data)調用它。

你的模型,確保calendar_id是你可填寫陣列的一部分:

protected $fillable = [ 
    'services_id', 
    'appt_time', 
    'total', 
    'calendar_id' 
]; 
+0

非常感謝!實際上是因爲我忘了將它添加到$ fillable中,現在很明顯。我想我之前並沒有把它稱爲靜態方法,因爲我遇到了問題,我只是在調試並忘記將其改回,但我也這樣做了。再次感謝。 –