doumianfeng6979 2017-10-24 03:08
浏览 279
已采纳

在数组laravel上调用成员函数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?

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(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 (how I show the image in view)

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

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(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>
  • 写回答

2条回答 默认 最新

  • duan3019 2017-10-24 03:15
    关注

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

    Instead get the file using the file() method:

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

    See more on the Laravel's requests documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器