我在laravel中有一个函数用于将数据保存到数据库中,由于某种原因,其中一个变量不会添加到数据库中,特别是日历的id。我已经尝试在数据库条目之前和之后返回变量,并且它在两次都存在,它只是不会被添加。以下是我到目前为止的情况:
from multiprocessing import Pool, cpu_count
from functools import partial
def updatePheromone(listOfDicts):
# Graph is accesible within this function's scope
pool = Pool()
partial_Func = partial(processDict, graph=graph)
pool.map_async(partial_Func, listOfDicts, len(listOfDicts)/cpu_count())
pool.close()
所有其他数据都已正确添加到数据库中,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.
}
}
除了它所说的特定错误"一般错误:1364字段' calendar_id'没有默认值。"当我允许空值时,它只添加" null"作为数据库中的值。这是它在数据库中的样子(这是MYSQL):
calendar_id.
答案 0 :(得分:0)
'calendar_id'没有默认值
$calendarID
未被传递,可能为null,请使用dd($calendarID)
确保设置。
create()
也是一个静态方法,所以不要创建Schedule
的实例
你可以静态地称呼Schedule::create($data)
。
在您的模型上,确保calendar_id
是您的可填充数组的一部分:
protected $fillable = [
'services_id',
'appt_time',
'total',
'calendar_id'
];