在laravel 5.4中不能使用stdClass类型的对象作为数组错误

时间:2017-07-08 11:53:02

标签: php arrays laravel-5.4

当我使用带有map功能的id获取图像时我使用Laravel 5.4并返回图像目录我收到此错误

这是控制器内部的功能

 $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;
           }           
         });

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

1 个答案:

答案 0 :(得分: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
           }           
         });