【问题标题】:Cannot use object of type stdClass as array error in laravel 5.4在 laravel 5.4 中不能使用 stdClass 类型的对象作为数组错误
【发布时间】:2017-07-08 11:53:02
【问题描述】:

我使用 Laravel 5.4 使用 id 和 map 函数获取图像并返回图像目录时出现此错误

这是控制器内部的函数

 $shipping = DB::table('shipping')
            ->select('shipping.id','products.name','shipping.is_return','shipping.pro_id as pid','shipping.tracking_url')
            ->join('products','shipping.pro_id','=','products.id')
            ->orderBy('shipping.id', 'desc')
            ->limit('5')->get();

        $shipping->map(function ($ship) {
           $directory = 'uploads/products/images/'.$ship->pid;
           if (is_dir($directory)) {
            $files = scandir ($directory);
            $img_file = $directory.'/'.$files[2];
            $ship->front_img = $img_file;
            print_r($ship['front_img']);exit;
           }           
         });

当我尝试打印输出时,它会显示错误。

【问题讨论】:

  • 改为$ship->front_img
  • 试过也...出现同样的错误

标签: php arrays laravel-5.4


【解决方案1】:

您正在尝试使用数组获取值,但 $ship 是对象

$shipping = DB::table('shipping')->select('shipping.id','products.name','shipping.is_return','shipping.pro_id as pid','shipping.tracking_url')
            ->join('products','shipping.pro_id','=','products.id')
            ->orderBy('shipping.id', 'desc')
            ->limit('5')->get();

        $shipping->map(function ($ship) {
           $directory = 'uploads/products/images/'.$ship->pid;
           if (is_dir($directory)) {
            $files = scandir ($directory);
            $img_file = $directory.'/'.$files[2];
            $ship->front_img = $img_file;
            print_r($ship->front_img);exit; //change here
           }           
         });

【讨论】:

    猜你喜欢
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 2021-12-17
    • 2012-01-19
    相关资源
    最近更新 更多