Karthiga
Karthiga

Reputation: 897

Cannot use object of type stdClass as array error in laravel 5.4

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.

Upvotes: 0

Views: 434

Answers (1)

B. Desai
B. Desai

Reputation: 16436

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

Upvotes: 1

Related Questions