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

时间:2017-10-24 03:08:37

标签: php laravel crud one-to-one

我尝试将一个图像从一个到多个关系发布,同时也在做CRUD(创建部分),但我在做这件事时遇到了一些麻烦。每当我尝试使用associate来定义与user_info和user_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(); $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'); } 。那我该怎么办?

以下是我的代码: createController:

public function getInfo($id) {

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

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

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

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

    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');
    }
}

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

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

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

{{1}}

2 个答案:

答案 0 :(得分:1)

我认为你在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

答案 1 :(得分:0)

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

$file = array();

$fileName = $file->getClientOriginalName();