在laravel 5.4中不能使用stdClass类型的对象作为数组错误(Cannot use object of type stdClass as array error in laravel 5.

编程入门 行业动态 更新时间:2024-10-26 14:35:26
在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 } });

更多推荐

本文发布于:2023-07-19 21:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1187564.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   中不   对象   错误   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!