gpt4 book ai didi

php - Laravel 实例错误

转载 作者:搜寻专家 更新时间:2023-10-31 21:05:00 26 4
gpt4 key购买 nike

我正在关注 dropzone 视频 ( https://laracasts.com/series/build-project-flyer-with-me/episodes/11 )。在我的例子中,一个用户有很多文件。

但我遇到了以下异常:

Argument 1 passed to App\User::addFile() must be an instance of App\files, instance of Symfony\Component\HttpFoundation\File\UploadedFile given, called in D:\wamp\www\local\app\Http\Controllers\UploadCenterController.php on line 28 and defined

我的 storeFiles Controller :

 public function storeFiles(Request $request)
{
$this->validate($request, [
'file' => 'required|mimes:jpg,jpeg,png,bmp'
]);
$file = $request->file('file');
$name = time() . $file->getClientOriginalName();
$file->move('files/user',$name);
$saveFile = files::fromForm($file);
$user = Auth::user()->addFile($saveFile);
}

文件模型中的静态方法 fromForm:

  public static function fromForm(UploadedFile $file)
{
$newFile = new static;
$name = time() . $file->getClientOriginalName();
$newFile->path = '/files/user/'.$name;
return $file;
}

并在用户模型中添加文件:

 public function addFile(files $file)
{
return $this->files()->save($file);
}

请帮忙!

--编辑--

整个文件类:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class files extends Model
{
protected $table = 'user_images';
protected $fillable = ["user_id","path"];

public function user()
{
return $this->belongsTo('App\User');
}

public static function fromForm(UploadedFile $file)
{
$newFile = new static;
$name = time() . $file->getClientOriginalName();
$newFile->path = '/files/user/'.$name;
return $file;
}
}

最佳答案

找到了!

必须在我的文件类中返回(fromForm 方法)

$newFile 而不是 $file!

关于php - Laravel 实例错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34111199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com