在laravel 5.4中不能使用stdClass类型的对象作为数组错误(Cannot use object of type stdClass as array error in 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; } });当我尝试打印输出时,它显示错误。
I am using Laravel 5.4 when I get the image using id with map function and return the image directory I got this error
This is function inside controller
$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; } });When I try to print the output it shows the error.
最满意答案
您正在尝试使用数组获取值,但$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 } });You are trying to get value using array but $ship is object
$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 } });更多推荐
发布评论