【问题标题】:Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, object given, called in laravel类型错误:传递给 Illuminate\Database\Eloquent\Builder::create() 的参数 1 必须是数组类型,给定对象,在 laravel 中调用
【发布时间】:2017-10-24 02:35:12
【问题描述】:

我正在尝试发布 一对多 关系的图像,同时还执行 CRUD(创建部分),但我在执行此操作时遇到了一些麻烦。我不断收到此错误

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

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

创建控制器:

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>

【问题讨论】:

  • 您是否阅读了错误信息?这是说当您必须传递一个数组时,您正在将一个对象传递给create() 方法...这意味着您不能这样做$UserImage = UserImage::create($file);
  • 但我确实使用了这个数组,$file = array();
  • 不,您将变量设置为空数组,然后在将其设置为文件时将其覆盖。
  • @James 我已经根据 Suraj 的回答更新了我的问题,但我遇到了一个新错误,你能帮我看一下吗,tks 很多
  • 你真的应该问这个问题,因为现在@suraj 的回答毫无意义。你读过你的代码吗?放慢速度,花一些时间来了解正在发生的事情。您正在创建一个名为 $file 的变量作为数组。然后你用它做什么?

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


【解决方案1】:

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

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

【讨论】:

  • 嗨 Suraj,感谢您的快速回复,在使用您的答案后,我收到了这个新错误,“调用数组上的成员函数 getClientOriginalName()”
  • 我想你正在保存文件名,你应该通过 $fileName = $file-&gt;getClientOriginalName() ; $UserImage = UserImage::create(['userImage' =&gt; $fileName]); 。确保提及正确的数据库列名称
  • 对不起,我不太明白,你能用更简单的术语解释一下吗
  • H Suraj,我更改了 $request->file('input_img')]);到 $request->file('$fileName')]);我还检查它是否在正确的数据库库中
猜你喜欢
  • 2018-04-19
  • 1970-01-01
  • 2020-02-23
  • 2018-05-28
  • 2017-12-17
  • 2019-03-04
  • 2020-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多