不能在laravel使用类型为stdClass的对象作为数组错误5.4
问题描述:
我使用Laravel 5.4,当我使用的ID与地图功能获取图像,并返回图像目录,我得到这个错误不能在laravel使用类型为stdClass的对象作为数组错误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;
}
});
当我尝试打印输出时显示错误。
答
您尝试使用数组中获取价值,但$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
}
});
做'$船 - > front_img'代替 – colburton
试过,还...发生了同样的错误 – Karthiga