调用未定义的方法Illuminate \ Database \ Eloquent \ Builder :: getAbsolutePath()

时间:2019-06-21 14:05:16

标签: php laravel eloquent

我有点困惑,我得到Call to undefined method Illuminate\Database\Eloquent\Builder::getAbsolutePath() (View: C:\Users\rust\Desktop\projectName\resources\views\container\index.blade.php)

我的show中的ContainerController.php方法看起来像这样:

public function show($args = '')
    {
        if($document = Document::where('key', $args)->first()) {
            return response()->download(storage_path('app'). DIRECTORY_SEPARATOR . $document->getAbsolutePath());
        }

        $items = explode('/', $args);
        $department = Auth::user()->department;

        if (Auth::user()->hasRole('Root')) {
            $department = Department::where('slug', '=', $items[0])->first();
            $items = array_slice($items, 1);
        }

        if($department == null) {
            return redirect('/');
        }

        if(file_exists(storage_path('app' . DIRECTORY_SEPARATOR . $department->slug . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $items)))
            && !is_dir(storage_path('app' . DIRECTORY_SEPARATOR . $department->slug . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $items)))) {

            return response()->file(storage_path('app' . DIRECTORY_SEPARATOR . $department->slug . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $items)));
        }

        $current_parent = 0;
        foreach ($items as $item) {
            $container = Container::where('slug', '=', $item)
                ->where('parent_id', '=', $current_parent)
                ->where('department_id', '=', $department->id)
                ->first();
            if(!$container) abort(404);
            $current_parent = $container->id;
        }

        $contents = Container::where('parent_id', '=', $current_parent)
            ->where('department_id', "=", $department->id)
            ->get();
        if (!($container = Container::find($current_parent))) {
            $container = $department;
        }

        return view('container.index', compact('container', 'contents', 'args'));
    }

我不知道我做错了什么,但我现在有点迷茫

1 个答案:

答案 0 :(得分:0)

在您的代码中... $document是laravel集合。不是文件对象。尝试加载文件,然后使用该文件调用getAbsolutePath()

// code snippts.

if($document = Document::where('key', $args)->first()) {
            return response()->download(storage_path('app'). DIRECTORY_SEPARATOR . $document->getAbsolutePath());
}

已编辑:view中,您还使用了 $container->getAbsolutePath(),第65行。