首页 > 解决方案 > AssetController.php 第 21 行中的 ErrorException:试图获取非对象的属性

问题描述

我有这个代码源,我试图在我的一个项目中使用它,它与 laravel 5.2 一起使用。这是assetController中的函数:

namespace App\Http\Controllers;

use App\Setting;
use File;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AssetController extends Controller
{
    /**
     * List all image from image directory
     */
    public function getAsset()
    {
        //Get Admin Images
        $adminImages = array();
        //get image file array
        $images_dir = Setting::where('name', 'images_dir')->first();
        $folderContentAdmin = File::files($images_dir->value);

        //check the allowed file extension and make the allowed file array
        $allowedExt = Setting::where('name', 'images_allowedExtensions')->first();
        $temp = explode('|', $allowedExt->value);
        foreach ($folderContentAdmin as $key => $item)
        {
            if( ! is_array($item))
            {
                //check the file extension
                $ext = pathinfo($item, PATHINFO_EXTENSION);
                //prep allowed extensions array
                if (in_array($ext, $temp))
                {
                    array_push($adminImages, $item);
                }
            }
        }

        //Get User Images
        $userImages = array();
        $userID = Auth::user()->id;
        $images_uploadDir = Setting::where('name', 'images_uploadDir')->first();
        if (is_dir( $images_uploadDir->value . "/" .$userID ))
        {
            $folderContentUser = File::files($images_uploadDir->value . "/" .$userID );
            if ($folderContentUser)
            {
                foreach ($folderContentUser as $key => $item)
                {
                    if ( ! is_array($item))
                    {
                        //check the file extension
                        $ext = pathinfo($item, PATHINFO_EXTENSION);
                        //prep allowed extensions array
                        //$temp = explode("|", $this->config->item('images_allowedExtensions'));
                        if (in_array($ext, $temp))
                        {
                            array_push($userImages, $item);
                        }
                    }
                }
            }
        }

        //var_dump($folderContent);
        //var_dump($adminImages);

        return view('assets/images', compact('adminImages', 'userImages'));
    }

问题出在第 21 行:

//get image file array
        $images_dir = Setting::where('name', 'images_dir')->first();
        $folderContentAdmin = File::files($images_dir->value);

从我的研究中我发现原因是因为设置表是空的,这是真的。如果不是这种情况,请告诉我是否还有其他原因导致该问题我需要解决方案,因为除了从数据库本身(phpmyAdmin)之外,我没有办法填充该表

标签: laravellaravel-5.2

解决方案


推荐阅读