gpt4 book ai didi

php - 传递给函数的 laravel 值不会传递到数据库中

转载 作者:行者123 更新时间:2023-11-30 21:59:35 26 4
gpt4 key购买 nike

我在 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 之外,所有其他数据都已正确添加到数据库中。它说的具体错误是“一般错误:1364 字段‘calendar_id’没有默认值。”当我允许空值时,它只会将“null”添加为数据库中的值。这是它在数据库中的样子(这是 MYSQL):

Name: calendar_id

Type: varchar(191) utf8mb4_unicode_ci

Null: Yes

Default: None

最佳答案

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

关于php - 传递给函数的 laravel 值不会传递到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43838401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com