在数组laravel上调用成员函数getClientOriginalName() [英] Call to a member function getClientOriginalName() on array laravel

查看:72
本文介绍了在数组laravel上调用成员函数getClientOriginalName()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一对多关系发布图像,同时也进行CRUD(创建零件),但是这样做有些麻烦.每当我尝试使用associate和user_info与user_image表一起定义关系时,我都会不断收到此错误,调用数组上的成员函数getClientOriginalName().那我该怎么办?

I'm trying to post an image from a one to many relationship while also doing the CRUD (create part), but I am having some trouble doing it. I keep on getting this error,Call to a member function getClientOriginalName() on array, whenever I try to use associate to define the relationship together with user_info with user_image table. So what should I do?

这是我的代码:createController:

Here are my codes: 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();



        $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(这是我打印信息的地方)

HomeController(this is where I print out my information)

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(我如何在视图中显示图像)

blade.php (how I show the image in view)

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

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

UserImage model(in table I used binary format to store in 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(这就是我上传图片的方式)

create1.blade.php(this is how I upload the image)

     <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 .

I think you have a problem inside store1() method. You sould not re-declare $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的请求文档.

这篇关于在数组laravel上调用成员函数getClientOriginalName()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆