laravel 8 中的文件上传 - 在 null 上调用成员函数 getClientOriginalName() 时出错
Posted
技术标签:
【中文标题】laravel 8 中的文件上传 - 在 null 上调用成员函数 getClientOriginalName() 时出错【英文标题】:File Upload in laravel 8 - Error Call to a member function getClientOriginalName() on null 【发布时间】:2021-11-15 02:10:19 【问题描述】:我有一个课程管理 CRUD。
文件上传给我以下错误
在 null 上调用成员函数 getClientOriginalName() 时出错
我在更新没有附件的课程时遇到了这个问题。
谁能帮我理解我在这里做错了什么? PS。我是 Laravel 和 MVC 框架的新手。
提前致谢。
存储和更新方法
public function CourseManagementStore(Request $request)
$validatedData =$request->validate([
'coursename' => 'required|unique:course_management',
'file' => 'required|mimes:doc,docx,csv,txt,xlx,xls,pdf|max:2048',
]);
$name = $request->file('file')->getClientOriginalName();
$path = $request->file('file')->store('public/uploads/coursedocs');
$data = new CourseManagement();
$data->coursename = $request->coursename;
$data->courseshortname = $request->courseshortname;
$data->awardingbody = $request->awardingbody;
$data->coursecategory = $request->coursecategory;
$data->courseduration = $request->courseduration;
$data->issuedate = $request->issuedate;
$data->validtill = $request->validtill;
$data->molapprovaldate = $request->molapprovaldate;
$data->molvalidtilldate = $request->molvalidtilldate;
$data->feestrainingonly = $request->feestrainingonly;
$data->feesexamonly = $request->feesexamonly;
$data->feesindividual = $request->feesindividual;
$data->feestamkeen = $request->feestamkeen;
$data->feesmol = $request->feesmol;
$data->feescompanysponsor = $request->feescompanysponsor;
$data->courseref = $request->courseref;
$data->molref = $request->molref;
$data->courseregstatus = $request->courseregstatus;
$data->coursedescription = $request->coursedescription;
$data->coursedocdesc = $request->coursedocdesc;
$data->trainer = $request->trainer;
$data->name = $name;
$data->path = $path;
$data->save();
$notification = array (
'message' => 'Course Added successfully',
'alert-type' => 'success'
);
return redirect()->route('course.viewall')->with($notification);
public function CourseManagementUpdate(Request $request, $id)
$data = CourseManagement::find($id);
$name = $request->file('file')->getClientOriginalName();
$path = $request->file('file')->store('public/uploads/coursedocs');
$data = new CourseManagement();
$data->coursename = $request->coursename;
$data->courseshortname = $request->courseshortname;
$data->awardingbody = $request->awardingbody;
$data->coursecategory = $request->coursecategory;
$data->courseduration = $request->courseduration;
$data->issuedate = $request->issuedate;
$data->validtill = $request->validtill;
$data->molapprovaldate = $request->molapprovaldate;
$data->molvalidtilldate = $request->molvalidtilldate;
$data->feestrainingonly = $request->feestrainingonly;
$data->feesexamonly = $request->feesexamonly;
$data->feesindividual = $request->feesindividual;
$data->feestamkeen = $request->feestamkeen;
$data->feesmol = $request->feesmol;
$data->feescompanysponsor = $request->feescompanysponsor;
$data->courseref = $request->courseref;
$data->molref = $request->molref;
$data->courseregstatus = $request->courseregstatus;
$data->coursedescription = $request->coursedescription;
$data->coursedocdesc = $request->coursedocdesc;
$data->trainer = $request->trainer;
$data->name = $name;
$data->path = $path;
$data->save();
$notification = array (
'message' => 'Course updated successfully',
'alert-type' => 'success'
);
return redirect()->route('course.viewall')->with($notification);
更新课程表
<form method="post" action=" route('course.update', $editData->id ) " enctype="multipart/form-data" id="upload-file">
@csrf
<!-- Basic Forms -->
<!-- .box-header -->
<div class="box">
<div class="box-header with-border">
<h4 class="box-title">Edit Course</h4>
</div>
<!-- /.box-header -->
<!-- .box-body -->
<div class="box-body">
<div class="row"><!-- .box-body row-->
<div class="col"><!-- .box-body col-->
<div class="row">
<div class="col-12">
<div class="row"> <!-- start row -->
<div class="col-md-6"> <!-- start col 6 -->
<div class="form-group">
<h5>Course Name<span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="coursename" class="form-control" value=" $editData ->coursename " required data-validation-required-message="This field is required">
</div>
@error('coursename')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- start col 6 -->
<div class="col-md-6"> <!-- start col 6 -->
<div class="form-group">
<h5>Course Short Name<span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="courseshortname" value=" $editData ->courseshortname " class="form-control" >
</div>
@error('courseshortname')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- end col 6 -->
</div><!-- end row -->
<div class="row"> <!-- start row -->
<div class="col-md-6"> <!-- start col 6 -->
<div class="form-group">
<h5>Awarding Body <span class="text-danger">*</span></h5>
<select name="awardingbody" class="form-control select2" style="width: 100%;">
<option value=" $editData->awardingbody "> $editData->awardingbody </option>
@foreach($awardingbodies as $awardingbody)
<option>$awardingbody->name</option>
@endforeach
</select>
</select>
</div>
</div><!-- start col 6 -->
<div class="col-md-6"> <!-- start col 6 -->
<div class="form-group">
<h5>Course Category <span class="text-danger">*</span></h5>
<select name="coursecategory" class="form-control select2" style="width: 100%;">
<option value=" $editData->coursecategory "> $editData->coursecategory </option>
@foreach($coursecategories as $coursecategory)
<option>$coursecategory->name</option>
@endforeach
</select>
</div>
</div><!-- end col 6 -->
</div><!-- end row -->
<div class="row"> <!-- start row -->
<div class="col-md-3"> <!-- start col 3 -->
<div class="form-group">
<h5>Course Ref Number<span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="courseref" value=" $editData ->courseref " class="form-control" required data-validation-required-message="This field is required">
</div>
@error('courseref')
<span class="text-info"> $message </span>
@enderror
</div>
<div class="form-group">
<h5>MOL Ref Number<span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="molref" value=" $editData ->molref " class="form-control" required data-validation-required-message="This field is required">
</div>
@error('molref')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- end col 3 -->
<div class="col-md-3"> <!-- start col 3 -->
<div class="form-group">
<h5>Trainer<span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="trainer" value=" $editData ->trainer " class="form-control" required data-validation-required-message="This field is required">
</div>
@error('trainer')
<span class="text-info"> $message </span>
@enderror
</div>
<div class="form-group">
<h5>Course Duration<span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="courseduration" value=" $editData ->courseduration " class="form-control" required data-validation-required-message="This field is required">
</div>
@error('courseduration')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- end col 6 -->
<div class="col-md-6"> <!-- start col 6 -->
<div class="form-group">
<h5>Course Description <span class="text-danger">*</span></h5>
<div class="controls">
<textarea rows="5" cols="5" name="coursedescription" class="form-control" placeholder="Textarea"> $editData ->coursedescription </textarea>
</div>
@error('coursedescription')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- start col 6 -->
</div><!-- end row -->
</div> <!-- end col-->
</div><!-- end row -->
</div><!--/ .box-body col-->
</div><!--/ .box-body row-->
</div><!--/ .box-body -->
</div> <!--/ .box -->
<div class="box"> <!-- .box -->
<div class="box-header with-border">
<h4 class="box-title">Fees</h4>
</div>
<div class="box-body"><!-- .box-body -->
<div class="row"> <!-- .box-body row -->
<div class="col"> <!-- .box-body col -->
<div class="row"> <!-- start row -->
<div class="col-md-2"> <!-- start col 2 -->
<div class="form-group">
<h5>Training Only<span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="feestrainingonly" value=" $editData ->feestrainingonly " class="form-control" required data-validation-required-message="This field is required">
</div>
@error('feestrainingonly')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- end col 2 -->
<div class="col-md-2"> <!-- start col 2 -->
<div class="form-group">
<h5>Exam Only<span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="feesexamonly" value=" $editData ->feesexamonly " class="form-control" required data-validation-required-message="This field is required">
</div>
@error('feesexamonly')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- end col 2 -->
<div class="col-md-2"> <!-- start col 2 -->
<div class="form-group">
<h5>Individual Fees<span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="feesindividual" value=" $editData ->feesindividual " class="form-control" required data-validation-required-message="This field is required">
</div>
@error('feesindividual')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- end col 2 -->
<div class="col-md-2"> <!-- start col 2 -->
<div class="form-group">
<h5>Tamkeen Fees<span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="feestamkeen" value=" $editData ->feestamkeen " class="form-control" required data-validation-required-message="This field is required">
</div>
@error('feestamkeen')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- end col 2 -->
<div class="col-md-2"> <!-- start col 2 -->
<div class="form-group">
<h5>MOL Fees<span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="feesmol" value=" $editData ->feesmol " class="form-control" required data-validation-required-message="This field is required">
</div>
@error('feesmol')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- end col 2 -->
<div class="col-md-2"> <!-- start col 2 -->
<div class="form-group">
<h5>Company Sponsor Fees<span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="feescompanysponsor" value=" $editData ->feescompanysponsor " class="form-control" required data-validation-required-message="This field is required">
</div>
@error('feescompanysponsor')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- end col 2 -->
</div><!-- end row -->
</div><!--/ .box-body col-->
</div><!--/ .box-body row-->
</div><!--/ .box-body -->
</div> <!--/ .box -->
<div class="box"> <!-- .box -->
<div class="box-header with-border">
<h4 class="box-title">Issue & Validation Dates</h4>
</div>
<div class="box-body"><!-- .box-body -->
<div class="row"> <!-- .box-body row -->
<div class="col"> <!-- .box-body col -->
<div class="row"> <!-- start row -->
<div class="col-md-6"> <!-- start col 6 -->
<div class="form-group">
<h5>MOL Approval Issue Date <span class="text-danger">*</span></h5>
<div class="controls">
<input type="date" name="molapprovaldate" value=" $editData ->molapprovaldate " class="form-control" >
</div>
@error('molapprovaldate')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- start col 6 -->
<div class="col-md-6"> <!-- start col 6 -->
<div class="form-group">
<h5>MOL Approval Valid Till <span class="text-danger">*</span></h5>
<div class="controls">
<input type="date" name="molvalidtilldate" value=" $editData ->molvalidtilldate " class="form-control" >
</div>
@error('molvalidtilldate')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- end col 6 -->
</div><!-- end row -->
<div class="row"> <!-- start row -->
<div class="col-md-6"> <!-- start col 6 -->
<div class="form-group">
<h5>Issue Date <span class="text-danger">*</span></h5>
<div class="controls">
<input type="date" name="issuedate" class="form-control" value=" $editData ->issuedate " required data-validation-required-message="This field is required">
</div>
@error('issuedate')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- start col 6 -->
<div class="col-md-6"> <!-- start col 6 -->
<div class="form-group">
<h5>Valid Till <span class="text-danger">*</span></h5>
<div class="controls">
<input type="date" name="validtill" value=" $editData ->validtill " class="form-control" >
</div>
@error('validtill')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- end col 6 -->
</div><!-- end row -->
</div><!--/ .box-body col-->
</div><!--/ .box-body row-->
</div><!--/ .box-body -->
</div> <!--/ .box -->
<div class="box"> <!-- .box -->
<div class="box-header with-border">
<h4 class="box-title">Documents</h4>
</div>
<div class="box-body"><!-- .box-body -->
<div class="row"> <!-- .box-body row -->
<div class="col"> <!-- .box-body col -->
<div class="row"> <!-- start row -->
<div class="col-md-6"> <!-- start col 6 -->
<div class="form-group">
<h5>Document Name <span class="text-danger">*</span></h5>
<div class="controls">
<input type="file" name="file" id="file" class="form-control" >
</div>
@error('Course Document')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- start col 6 -->
<div class="col-md-6"> <!-- start col 6 -->
<div class="form-group">
<h5>Description <span class="text-danger">*</span></h5>
<div class="controls">
<textarea rows="3" cols="5" name="coursedocdesc" class="form-control" placeholder="Textarea"></textarea>
</div>
@error('name')
<span class="text-info"> $message </span>
@enderror
</div>
</div><!-- end col 6 -->
</div><!-- end row -->
</div><!--/ .box-body col-->
</div><!--/ .box-body row-->
</div><!--/ .box-body -->
</div> <!--/ .box -->
<div class="box"> <!-- .box -->
<div class="box-header with-border">
<h4 class="box-title">Course Status</h4>
</div>
<div class="box-body"><!-- .box-body -->
<div class="row"> <!-- .box-body row -->
<div class="col"> <!-- .box-body col -->
<div class="row"> <!-- start row -->
<div class="col-md-12"> <!-- start col 12 -->
<div class="form-group">
<h5>Course Registration Status <span class="text-danger">*</span></h5>
<div class="controls">
<select name="courseregstatus" id="select" required class="form-control">
<option value=" $editData ->courseregstatus "> $editData ->courseregstatus </option>
<option value="Pending">Pending</option>
<option value="Valid">Valid</option>
<option value="Expired">Expired</option>
<option value="Processing-Renewal">Processing Renewal</option>
<option value="Course-Khallas">Course Khallas!</option>
</select>
</div>
</div>
</div><!-- start col 12 -->
</div><!-- end row -->
</div><!--/ .box-body col-->
</div><!--/ .box-body row-->
</div><!--/ .box-body -->
</div> <!--/ .box -->
<div class="text-xs-right">
<input type="submit" class="btn btn-info btn-rounded mb-5" value="Update">
</div>
</form>
【问题讨论】:
$name = $request->file->getClientOriginalName(); $path = $request->file->store('public/uploads/coursedocs');尝试不使用 file() 函数。 @Cameron 不。得到同样的错误。 【参考方案1】:我认为你可以使用 optional() 助手
$name = optional($request->file('file'))->getClientOriginalName();
$path = optional($request->file('file'))->store('public/uploads/coursedocs');
如果对象不存在,它会返回 null 并且不会出错。 信息在这里https://laravel.com/docs/8.x/helpers#method-optional
【讨论】:
这也有效.. 谢谢 我今天刚刚通知了这个辅助函数。谢谢。【参考方案2】:由于添加文件不是强制性的,因此您必须测试您的$request->file('file')
是否为null 或不是。因此,将其添加到您的代码中以避免错误:
public function CourseManagementUpdate(Request $request, $id)
$data = CourseManagement::find($id);
$data->coursename = $request->coursename;
$data->courseshortname = $request->courseshortname;
$data->awardingbody = $request->awardingbody;
$data->coursecategory = $request->coursecategory;
$data->courseduration = $request->courseduration;
$data->issuedate = $request->issuedate;
$data->validtill = $request->validtill;
$data->molapprovaldate = $request->molapprovaldate;
$data->molvalidtilldate = $request->molvalidtilldate;
$data->feestrainingonly = $request->feestrainingonly;
$data->feesexamonly = $request->feesexamonly;
$data->feesindividual = $request->feesindividual;
$data->feestamkeen = $request->feestamkeen;
$data->feesmol = $request->feesmol;
$data->feescompanysponsor = $request->feescompanysponsor;
$data->courseref = $request->courseref;
$data->molref = $request->molref;
$data->courseregstatus = $request->courseregstatus;
$data->coursedescription = $request->coursedescription;
$data->coursedocdesc = $request->coursedocdesc;
$data->trainer = $request->trainer;
if ( isset($request->file('file')) != null )
$data->name = $request->file('file')->getClientOriginalName();
$data->path = $request->file('file')->store('public/uploads/coursedocs');
$data->save();
$notification = array (
'message' => 'Course updated successfully',
'alert-type' => 'success'
);
return redirect()->route('course.viewall')->with($notification);
【讨论】:
看起来$data = new CourseManagement();
不应该在那里。你实际上已经通过 find 分配了 $data 然后你完全覆盖它(只是为了增强这个答案)
@Frnak 我更正了,谢谢
@atika 我现在得到以下错误,当我加载视图时` Symfony\Component\ErrorHandler\Error\FatalError 不能在表达式的结果上使用 isset() (你可以使用“null !== 表达式") `
试试if ( null != $request->file('file') )
- isset 在这里不起作用
@Frnak 效果很好。百万谢谢。以上是关于laravel 8 中的文件上传 - 在 null 上调用成员函数 getClientOriginalName() 时出错的主要内容,如果未能解决你的问题,请参考以下文章
在Laravel中通过AngularJs上传文件时获取值为null