[英]laravel value passed to function won't get passed into Database
我在laravel中有一個函數用於將數據保存到數據庫中,由於某種原因,其中一個變量不會添加到數據庫中,特別是日歷的id。 我已經嘗試在數據庫條目之前和之后返回變量,並且它在兩個時間都存在,它不會被添加。 這是我到目前為止:
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.
之外,所有其他數據都會正確添加到數據庫中calendar_id.
它說的具體錯誤是“一般錯誤:1364字段'calendar_id'沒有默認值。” 當我允許空值時,它只在數據庫中添加“null”作為值。 這是它在數據庫中的樣子(這是MYSQL):
Name: calendar_id
Type: varchar(191) utf8mb4_unicode_ci
Null: Yes
Default: None
'calendar_id'沒有默認值
$calendarID
沒有被傳遞,也許它是null,使用dd($calendarID)
來確保它被設置。
create()
也是一個靜態方法,所以不要創建一個Schedule
實例,因為你可以靜態調用它來Schedule::create($data)
。
在您的模型上,確保calendar_id
是可填充數組的一部分:
protected $fillable = [
'services_id',
'appt_time',
'total',
'calendar_id'
];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.