文件上传在laravel 8 -错误调用成员函数getClientOriginalName()为空



我有一个课程管理CRUD。

文件上传给我以下错误

调用成员函数getClientOriginalName()为null

我在没有附件的情况下更新课程时遇到了这个问题。

谁能帮我理解我在这里做错了什么?附言:我刚来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>

由于添加文件不是强制性的,您必须测试您的$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);
}

我认为你可以使用可选的()帮助器

$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

相关内容

  • 没有找到相关文章