Laravel 的数据库更新请求失败
Failed DB update requests with Laravel
在为此花费大量时间并在线搜索答案后,我非常坚持使用 Laravel 进行看似简单的数据库更新。这是我的一个控制器中有缺陷的功能的(简化)版本:
public function changeDetails(Request $request)
{
...
// It works to change member_id into another...
if (Request::input('recipient_id') != "null") {
DB::table('recipients')
->where('recipients.id','=',Request::input('recipient_id'))
->update([
'recipients.member_id' => Request::input('member_id')
]);
// ...but it won't let me change it to NULL.
} else {
DB::table('recipients')
->where('recipients.id','=',Request::input('recipient_id'))
->update([
'recipients.member_id' => null
]);
};
我最初认为问题与数据库或 table 有关,尤其是因为 'member_id' 是外键。然而,我做了两个证明并非如此的测试。首先,我确定该列是 'unsigned' 和 'nullable'。其次,我手动将一个整数插入到“where”子句中而不是“Request::input('recipient_id')”...并且它工作正常。我还确认“Request::input('recipient_id')”的值确实是一个整数,它应该在适当的列(即 'bigint' 类型)内工作。
有用吗suggestions/observations?他们将不胜感激。
只有当Request::input('recipient_id')为“null”时,代码才会进入else语句,
但是您的 else 语句也使用 'recipient_id' ,它是“null”。
因此您的 SQL 语句将找到 recipients.id = "null" 的收件人。
显然你没有任何“recipient_id”是“null”,对吧?
...
else {
DB::table('recipients')
->where('recipients.id','=',Request::input('recipient_id')) // here the recipient_id is "null"
->update([
'recipients.member_id' => null
]);
};
...
在为此花费大量时间并在线搜索答案后,我非常坚持使用 Laravel 进行看似简单的数据库更新。这是我的一个控制器中有缺陷的功能的(简化)版本:
public function changeDetails(Request $request)
{
...
// It works to change member_id into another...
if (Request::input('recipient_id') != "null") {
DB::table('recipients')
->where('recipients.id','=',Request::input('recipient_id'))
->update([
'recipients.member_id' => Request::input('member_id')
]);
// ...but it won't let me change it to NULL.
} else {
DB::table('recipients')
->where('recipients.id','=',Request::input('recipient_id'))
->update([
'recipients.member_id' => null
]);
};
我最初认为问题与数据库或 table 有关,尤其是因为 'member_id' 是外键。然而,我做了两个证明并非如此的测试。首先,我确定该列是 'unsigned' 和 'nullable'。其次,我手动将一个整数插入到“where”子句中而不是“Request::input('recipient_id')”...并且它工作正常。我还确认“Request::input('recipient_id')”的值确实是一个整数,它应该在适当的列(即 'bigint' 类型)内工作。
有用吗suggestions/observations?他们将不胜感激。
只有当Request::input('recipient_id')为“null”时,代码才会进入else语句, 但是您的 else 语句也使用 'recipient_id' ,它是“null”。
因此您的 SQL 语句将找到 recipients.id = "null" 的收件人。
显然你没有任何“recipient_id”是“null”,对吧?
...
else {
DB::table('recipients')
->where('recipients.id','=',Request::input('recipient_id')) // here the recipient_id is "null"
->update([
'recipients.member_id' => null
]);
};
...