调用数组 laravel 上的成员函数 getClientOriginalName()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46901573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to a member function getClientOriginalName() on array laravel
提问by blastme
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?
我正在尝试从一对多关系发布图像,同时还进行 CRUD(创建部分),但是我在做时遇到了一些麻烦。,Call to a member function getClientOriginalName() on array
每当我尝试使用关联来定义与 user_info 和 user_image 表的关系时,我都会不断收到此错误。所以我该怎么做?
Here are my codes: createController:
这是我的代码: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)
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 (how I show the image in view)
Blade.php(我如何在视图中显示图像)
@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)
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(this is how I upload the image)
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>
回答by Camilo
I think you have a problem inside store1()
method. You sould not re-declare $file
.
我认为您在store1()
方法内部有问题。你不能重新申报 $file
。
if($file = $request->hasFile('input_img')) {
$file = array();
$fileName = $file->getClientOriginalName();
Instead get the file using the file()
method:
而是使用以下file()
方法获取文件:
if($request->hasFile('input_img')) {
$file = $request->file('input_img');
$fileName = $file->getClientOriginalName();
See more on the Laravel's requests documentation.
在Laravel 的请求文档中查看更多信息。
回答by James
Read through your logic in the store1
method again... You are setting the $file
variable to an empty array and then trying to call the getClientOriginalName()
method on it:
store1
再次通读方法中的逻辑......您将$file
变量设置为一个空数组,然后尝试getClientOriginalName()
在其上调用该方法:
$file = array();
$fileName = $file->getClientOriginalName();