类型错误:传递给Illuminate \ Database \ Eloquent \ Builder :: create()的参数1必须是类型数组,给定对象,在laravel中调用

时间:2017-10-24 02:35:12

标签: php laravel one-to-many laravel-eloquent

我正在尝试从一个到多个关系发布图像,同时也在做CRUD(创建部分),但我在做这件事时遇到了一些麻烦。我继续收到此错误

  

类型错误:传递给Illuminate\Database\Eloquent\Builder::create()的参数1必须是类型数组,给定对象,在

中调用

每当我尝试使用关联来与 user_info 一起使用 user_image 表来定义关系时。我已经使用数组函数将它变成一个数组,但它仍然给了我这个错误。那我该怎么办?

createController:

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();

        $file = $request->file('input_img') ;
        $fileName = $file->getClientOriginalName() ;
        $destinationPath = public_path().'/images' ;
        $file->move($destinationPath,$fileName);
        $UserImage->userImage = $fileName ;
        $UserImage = UserImage::create($file);
        $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模型(表中我使用二进制格式存储在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>

1 个答案:

答案 0 :(得分:1)

您应该在传递数据时给出一个数组来创建这样的方法。目前,您正在传递文件对象。

$UserImage = UserImage::create(['file' => $request->file('input_img')]);