调用数组 laravel 上的成员函数 getClientOriginalName()

Call to a member function getClientOriginalName() on array laravel

我正在尝试从一对多关系 post 图像,同时还执行 CRUD(创建部分),但我在执行此操作时遇到了一些问题。每当我尝试使用 associate 来定义与 user_info 和 user_image table 的关系时,我一直收到此错误 ,Call to a member function getClientOriginalName() on array。那我该怎么办呢?

这是我的代码: 创建控制器:

public function create1(){

    return view('create1');
}

public function store1(Request $request){
     $this->validate($request, [
        'input_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

 $user_info = Session::get('data');
      $UserImage = new UserImage($request->input()) ;


     if($file = $request->hasFile('input_img')) {
      $file = array();



        $fileName = $file->getClientOriginalName() ;
        $destinationPath = public_path().'/images' ;
        $file->move($destinationPath,$fileName);
        $UserImage->userImage = $fileName ;
        $UserImage = UserImage::create(['file' => $request->file('input_img')]);
        $UserImage->user_infos()->associate($user_info);
    }

    $UserImage->save() ;

    return redirect('/home');
}

HomeController(这是我打印信息的地方)

public function getInfo($id) {

  $data = personal_info::where('id',$id)->get();
      $data3=UserImage::where('user_id',$id)->get();
 return view('test',compact('data','data3'));

blade.php(我如何在视图中显示图像)

 @foreach ($data3 as $object9)
 <img width="100" height="100" src="{!! $object9->signature !!}">
    @endforeach

UserImage模型(在table我使用二进制格式存储在DB中)

    class UserImage extends Eloquent
    {
            protected $fillable = array('userImage','user_id');
        public function user_infos() {
            return $this->belongsTo('App\user_info', 'user_id', 'id');
        }

class user_info extends Eloquent
{
    protected $fillable = array('Email', 'Name');
    protected $table = user_infos';
    protected $primaryKey = 'id';
        public function UserImages() {
        return $this->hasOne('App\UserImage','user_id');
    }
}

create1.blade.php(我是这样上传图片的)

     <form class="form-horizontal" method="post" action="{{ url('/userUpload')}}" enctype="multipart/form-data">

                        {{  csrf_field()  }}


<div class="form-group">
    <label for="imageInput" class="control-label col-sm-3">Upload Image</label>
            <div class="col-sm-9">
            <input data-preview="#preview" name="input_img" type="file" id="imageInput">
            <img class="col-sm-6" id="preview"  src="" ></img>
        </div>
    </div>

 <div class="form-group">
            <div class="col-md-6-offset-2">
              <input type="submit" class="btn btn-primary" value="Save">
            </div>
          </div>
          </form>

再次阅读您在 store1 方法中的逻辑...您正在将 $file 变量设置为一个空数组,然后尝试对其调用 getClientOriginalName() 方法:

$file = array();

$fileName = $file->getClientOriginalName();

我认为您在 store1() 方法内部有问题。你不应该重新声明 $file.

if($file = $request->hasFile('input_img')) {

    $file = array();

    $fileName = $file->getClientOriginalName();

而是使用 file() 方法获取文件:

if($request->hasFile('input_img')) {

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

    $fileName = $file->getClientOriginalName();

Laravel's requests documentation 上查看更多信息。