【问题标题】:Laravel DB Facade BadMethodCallExceptionLaravel DB 门面 BadMethodCallException
【发布时间】:2019-09-05 14:29:02
【问题描述】:

我正在尝试从 Laravel DB Facade 实现 insertOrIgnore 方法, 这是文档+说明 sn-p 的链接: https://laravel.com/docs/5.8/queries#inserts

insertOrIgnore 方法将忽略重复记录错误,同时 将记录插入数据库:

DB::table('users')->insertOrIgnore([
    ['id' => 1, 'email' => 'taylor@example.com'],
    ['id' => 2, 'email' => 'dayle@example.com']
]);

这是产生错误的一段代码(它适用于常规insert()

if ($datetime->format('H:i') >= '05:50' && $datetime->format('H:i') <= '07:10') {
    DB::table('attendanceTable')->insertOrIgnore(['user_id' => $request->loggedUserId, 'day' => $datetime,  'shift_id' => $Shifts->id, 'created_at' => $datetime, 'updated_at' => $datetime]);

这是 Laravel 的 Telescope 产生的错误

Call to undefined method Illuminate\Database\Query\Builder::insertOrIgnore()

有人可以指出我做错了什么,或者至少给我一个提示吗?提前致谢!

【问题讨论】:

  • 你使用的是哪个版本的 Laravel?
  • @KuldeepMishra 非常感谢!
  • @CaddyDZ 5.8,与文档相同。
  • 在 v. 5.8 和 6.0 中,函数的语法仅使用 Postgres、MySql 和 Sqlite 驱动程序定义,如果您使用 SqlServer 应该引发异常 This database engine does not support inserting while ignoring errors 而不是 Undefined Method

标签: php mysql laravel


【解决方案1】:

我遇到了同样的错误,原来是因为我在laravel版本5.8.32,而insertOrIgnore是在5.8.33版本中添加的。

运行 composer update 解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2021-10-06
    • 2013-10-28
    • 2020-05-03
    • 1970-01-01
    • 2017-10-27
    相关资源
    最近更新 更多