【发布时间】:2021-01-23 23:59:45
【问题描述】:
我刚刚将 laravel 升级到 v8,我正在尝试在播种机上运行记录在 here 上的 upsert 函数。
这是我正在运行的代码示例
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class NewspaperSeeder extends Seeder
{
/**
* Run the database seeders.
*
* @return void
*/
public function run()
{
DB::table("newspapers")->upsert(
["rows to insert"],
["primary key"],
["attributes to update if duplicate"]);
}
}
同时,当我运行 php artisan db:seed 时,我最终得到了这个错误
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::upsert()
at vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:50
46▕ * @throws \BadMethodCallException
47▕ */
48▕ protected static function throwBadMethodCallException($method)
49▕ {
➜ 50▕ throw new BadMethodCallException(sprintf(
51▕ 'Call to undefined method %s::%s()', static::class, $method
52▕ ));
53▕ }
54▕ }
• Bad Method Call: Did you mean Illuminate\Database\Query\Builder::insert() ?
编辑(composer.json):
我按照官方文档上的升级指南,运行composer update
{
"require": {
"php": "^7.2.5",
"ext-json": "^7.4",
"doctrine/dbal": "^2.10",
"fideloper/proxy": "^4.0",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^6.3",
"laravel/framework": "^8.0",
"laravel/legacy-factories": "^1.0",
"laravel/passport": "^10.0",
"laravel/socialite": "^5.0",
"laravel/tinker": "^2.0",
"laravel/ui": "^3.0"
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
"facade/ignition": "^2.3.6",
"filp/whoops": "^2.0",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^5.0",
"phpunit/phpunit": "^9.0"
},
}
【问题讨论】:
-
是的,在我链接的文档上搜索 upsert 就会找到
-
upsert 方法将插入不存在的行并用新值更新已经存在的行。该方法的第一个参数是要插入或更新的值,而第二个参数是唯一标识关联表中的记录的列。该方法的第三个也是最后一个参数是在数据库中已经存在匹配记录时应该更新的列:
-
您必须等待
laravel/framework的下一个标记版本才能使用该功能 -
我对 Laravel 8.9.0 也有同样的想法:
BadMethodCallExceptionCall to undefined method Illuminate\Database\Query\Builder::upsert() -
@PaulH Laravel 8.10 现已发布,请将您的版本升级到它,它现在应该可以工作了。检查github.com/laravel/framework/releases