调用未定义的方法File :: save()

时间:2019-01-08 12:15:25

标签: laravel-5 laravel-5.6

我已经检查了所有具有相同问题的线程,但仍然找不到为什么不起作用。我有这个功能

namespace App\Http\Controllers\Admin;

use App\MediaKit;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Validator ;
use Illuminate\Support\Facades\File;

class MediaKitController extends Controller
{
   public function store(Request $request)
   {
      $this->validate($request, [

            'filename' => 'required',
            'filename.*' => 'mimes:doc,pdf,docx,zip,png'
      ]);

    if($request->hasfile('filename'))
    {
        foreach($request->file('filename') as $files)
        {
            $name = $files->getClientOriginalName();
            $files->move(public_path().'/files/', $name);  
            $data[] = $name;  
        }
    }

    $files= new \File();
    $files->filename=json_encode($data);       
    $files->save();

    return back()->with('success', 'Your files has been successfully added');
}

当我尝试将文件保存到数据库时,出现以下错误。这些文件将保存在目录中。

  

“调用未定义的方法Illuminate \ Support \ Facades \ File :: save()”

1 个答案:

答案 0 :(得分:1)

  1. 将此use Illuminate\Support\Facades\File;替换为模型名称空间。
  2. 将此$files= new \File();替换为$files= new File();