请在尝试上传图片时收到此错误 fileUpload() 必须是 Illuminate\Http\Request 的实例

Please getting this error fileUpload() must be an instance of Illuminate\Http\Request when trying to upload an image

传递给 App\Candidate::fileUpload() 的参数 1 必须是 Illuminate\Http\Request 的实例,给定的 Illuminate\Http\UploadedFile 实例,在 C:\xampp\htdocs\Laravel-voting-system\app\Candidate.php 第 40

请问我不知道哪里错了 this is where I wrote the fill upload function(candidate.php)

The second image is the downward path of the same folder candidate.php

这是我的 user.php 供您查看

<?php

namespace App;


use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','regno'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

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

    public function candidate(){
        return $this->hasOne('App\Candidate');
    }

    public function registerVoter($name,$email,$password,$regno){
        $newVoter = new User;
        $newVoter->name = $name;
        $newVoter->email = $email;
        $newVoter->password = bcrypt($password);
        $newVoter->regno = $regno;
        $newVoter->role_id = 2;
        $newVoter->save();
    }
    public static function addCandidate($studentId,$seat,$image){
        $user = User::find($studentId);
        (new Candidate)->add($user->name,$seat,$user->regno,$user->id,$image,);
    }

还有我的AdminController.php

    <?php

namespace App;


use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','regno'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

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

    public function candidate(){
        return $this->hasOne('App\Candidate');
    }

    public function registerVoter($name,$email,$password,$regno){
        $newVoter = new User;
        $newVoter->name = $name;
        $newVoter->email = $email;
        $newVoter->password = bcrypt($password);
        $newVoter->regno = $regno;
        $newVoter->role_id = 2;
        $newVoter->save();
    }
    public static function addCandidate($studentId,$seat,$image){
        $user = User::find($studentId);
        (new Candidate)->add($user->name,$seat,$user->regno,$user->id,$image,);
    }

非常感谢您的努力,提前致谢。

您似乎混合了控制器和模型。

看,控制器是用来处理请求的。该模型旨在持久化实体并处理它们的关系。你的 Candidate 两者都不是。

Laravel 有一个类型提示系统,它会自动在控制器方法中注入一个提示类型的实例。因此,如果您拥有

这样的控制器
<?php
 use  Illuminate\Http\Request;

  class CandidateController {

    public function fileUpload( Request $request ) {
      ...
    }
  }

以及类似

的路线
Route::post('candidate','CandidateController@fileUpload');

然后 fileUpload 方法将接收 \Illuminate\Http\Request

的实例

现在,如果您从前端提交文件,该文件将在请求中(如您的代码所示)

$image = $request->file('image');  // 'image' is just the input name

所以你不应该从另一个方法调用 fileUpload。恰恰相反。前端发送请求,控制器处理请求和 "extracts" 文件,该文件随后将保存在 disk/cloud/wherever 中,其元数据发送到 DDBB 并关联到代理用户

控制器将图像(您已经在这样做)移动到其预期路径,然后将其存储在模型中,例如

  public function fileUpload( Request $request ) {

    $image = $request->file('image');  

    $candidate = new App\Candidate();

    $name = time().'.'.$image->getClientOriginalName();

    $image->move(public_path("images"), $name);
    $candidate->path = public_path('images').'/'.$name;
    $candidate->save();
  }

因为还有其他字段,我猜你也在请求中发送它们,比如

    $candidate->seat = $request->seat;

如果需要,代理用户应该来自身份验证助手(例如会话或令牌),以避免恶意访问者发送另一个用户的 ID。