Laravel AssetController.php第21行中的ErrorException:尝试获取非对象的属性
我在我的一个项目中尝试使用这个源代码,它与Laravel5.2一起使用。这是assetController中的功能:Laravel AssetController.php第21行中的ErrorException:尝试获取非对象的属性,laravel,laravel-5.2,Laravel,Laravel 5.2,我在我的一个项目中尝试使用这个源代码,它与Laravel5.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 a
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)填充表外,我没有其他方法填充该表。看起来您的
设置
表中的这些位置是空的。将->first()
替换为->firstOrFail()
,以确保您自己……是的,表是空的,我使用了firstOrFail(),这就是结果模型[App\Setting]没有查询结果。我不知道如何填充设置
表,但这是您的问题。你应该在那里有一些匹配列name
和值images\u dir
的属性,这是设置表中的属性:id name value default\u value description required created\u at updated\u atAs我说这个代码不是我的,我只是想通过添加更多功能在我的项目中使用它。现在我需要一种方法来继续,即使桌子是空的