【问题标题】:Please getting this error fileUpload() must be an instance of Illuminate\Http\Request when trying to upload an image尝试上传图像时,请收到此错误 fileUpload() must be an instance of Illuminate\Http\Request
【发布时间】:2020-08-07 12:55:54
【问题描述】:

传递给 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,);
    }

非常感谢您的努力,在此先感谢。

【问题讨论】:

标签: php laravel file-upload laravel-7


【解决方案1】:

看起来您已经将控制器与模型混合在一起了。

看,控制器是用来处理请求的。该模型旨在持久化实体并处理它们的关系。你的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。这是相反的方式。前端发送请求,控制器处理请求并“提取”文件,然后将其保存在磁盘/云/任何地方,并将其元数据发送到 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。

【讨论】:

  • 我非常感激。你的意思是我应该从模型中删除 Fileupload 函数并为它创建一个新的控制器。请问如果我添加到我的 AdminController 会怎样?
  • 检查对您的问题的评论:您在控制器应该在的位置重复了您的模型代码。无论如何,fileUpload 方法应该进入控制器并由路由隐式调用,而不是由另一个方法调用(除非所述方法可以访问请求本身)
  • 非常感谢,至少很明显,我在 Laravel 中站稳了脚跟,你看到我的代码真的很感激。我现在正在努力。
猜你喜欢
  • 2021-11-23
  • 2017-08-14
  • 2018-03-31
  • 2019-01-05
  • 1970-01-01
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多