【问题标题】:Error explode in LaravelLaravel 中的错误爆炸
【发布时间】:2017-01-13 08:57:54
【问题描述】:

我的代码是这样的。

$idwisa = "288";
$stemp = DB::table('t_hasil_temp')
    ->select('hasil')
    ->where('id', $idwisa)
    ->get();

print_r($stemp); 我得到Array ( [0] => stdClass Object ( [hasil] => 20,24,22,26 ) )hasil 是一个字符串

我想将hasil 转换为数组,我尝试这样。问题出在explode

$temp = explode(",",$stemp->hasil); // error Trying to get property of non-object

$temp = explode(",",$stemp); // error explode() expects parameter 2 to be string, array given

因为之后我会像这样使用$temp

$temps = Objek::whereIn('id',$temp)->get();

有什么解决办法吗?感谢您的关注。

【问题讨论】:

    标签: php mysql sql laravel laravel-5.2


    【解决方案1】:

    试试

    $temp = explode(",",$stemp[0]->hasil); 
    print_r($temp);
    

    因为您的数组是对象的多维数组。

    【讨论】:

      【解决方案2】:

      一定要使用->get()->first()

      在此处更改以获取单个对象

      $stemp = DB::table('t_hasil_temp')
         ->select('hasil')
         ->where('id', $idwisa)
         ->first();
      

      ->get() 给出了对象数组,尽管您有一个对象,但您需要添加数组的 0th 值。这里$stemp[0]

      【讨论】:

        【解决方案3】:

        ->get() 返回一个结果数组,看来您想使用只返回第一个找到的结果的->first()

        $stemp = DB::table('t_hasil_temp')
            ->select('hasil')
            ->where('id', $idwisa)
            ->first();
        

        另外,如果您有 HasilTemp 模型,您也可以使用:

        $stemp = HasilTemp::find($idwisa);
        

        【讨论】:

          【解决方案4】:

          尝试理解这个概念,当你使用时

          ->get();

          返回:

          Array ( [0] => stdClass Object ( [hasil] => 20,24,22,26 ) ) which is a stdClass Object
          

          你可以像这样访问它的元素:

          $stemp[0]->hasil;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-10-17
            • 1970-01-01
            • 1970-01-01
            • 2014-06-04
            • 1970-01-01
            • 2018-09-01
            • 1970-01-01
            • 2019-07-26
            相关资源
            最近更新 更多