【问题标题】:Object of class stdClass could not be converted to int Laravel 5.6类 stdClass 的对象无法转换为 int Laravel 5.6
【发布时间】:2018-07-09 10:24:35
【问题描述】:

我想在 laravel 5.6 中检查数据库中的记录,但我收到消息“stdClass 类的对象无法转换为 int”。

  $pengoreksi = DB::table('peserta')->latest('pengoreksi')->first();

  if($pengoreksi <= 8){
    $korektor = $pengoreksi++;
  }
  else {
    $korektor = 1;
  }

【问题讨论】:

  • $pengoreksi 不是数字

标签: php mysql laravel


【解决方案1】:

根据文档,

如果您只需要从数据库表中检索一行,您可以使用第一种方法。此方法将返回单个 StdClass 对象:

$user = DB::table('users')->where('name', 'John')->first();
echo $user->name;

不能直接使用$pengoreksi,先把值存入任意变量,再使用。

$pengoreksi = DB::table('peserta')->latest('pengoreksi')->first();
$pen = $pengoreksi->pengoreksi;
  if($pengoreksi <= 8){
    $korektor = $pen++;
  }
  else {
    $korektor = 1;
  }

【讨论】:

    【解决方案2】:

    在这一行中,您会得到包含许多列的 db 行,添加列名

    $pengoreksi = DB::table('peserta')->latest('pengoreksi')->first()->{**COLUMN_NAME**};
    

    【讨论】:

      【解决方案3】:

      这不起作用,因为您将检索一个对象。您需要访问对象中的值才能访问您的号码。

      $pengoreksi = DB::table('peserta')->latest('pengoreksi')->first();
      //If you have saved pangoreksi as a string in your database you must type hint (int) on $pengoreksi->pangoreksi 
      if($pengoreksi->pangoreksi <= 8){
          $korektor = $pengoreksi->pangoreksi++;
      }
      else {
          $korektor = 1;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-21
        • 2020-03-16
        • 1970-01-01
        • 1970-01-01
        • 2021-05-28
        • 1970-01-01
        相关资源
        最近更新 更多