在 null 上调用成员函数 getRealPath()
Call to a member function getRealPath() on null
我想问一下,如果没有上传的文件,但他们按下了上传按钮,我该如何输入错误消息。如果 getRealPath 为空,我如何输入错误消息?
public function importExcel()
{
if (empty(Input::file('import_file')->getRealPath())) {
return back()->with('success','No file selected');
}
else {
$path = Input::file('import_file')->getRealPath();
$inserts = [];
Excel::load($path,function($reader) use (&$inserts)
{
foreach ($reader->toArray() as $rows){
foreach($rows as $row){
$inserts[] = ['biometrics' => $row['biometrics'], 'first_name' => $row['first_name'], 'last_name' => $row['last_name'], 'date' => $row['date'], 'emp_in' => $row['emp_in'], 'emp_out' => $row['emp_out']];
}
}
});
if (!empty($inserts)) {
DB::table('attendances')->insert($inserts);
return back()->with('success','Inserted Record successfully');
}
return back();
}
}
您可以像下面这样设置错误信息
return redirect()->back()->with('errors', 'No file selected');
然后在 blade 中显示错误消息,就像在 laravel 文档中提到的那样。
它在 session 中存储消息,您可以轻松地在 blade 文件中显示会话消息,如下所示
@if (count($errors) > 0)
<!-- Form Error List -->
<div class="alert alert-danger error">
<strong>Whoops! Something went wrong!</strong>
<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
您可以在调用 getRealPath() 函数之前放置一个 if
if( Input::file('import_file') ) {
$path = Input::file('import_file')->getRealPath();
} else {
return back()->withErrors(...);
}
我想问一下,如果没有上传的文件,但他们按下了上传按钮,我该如何输入错误消息。如果 getRealPath 为空,我如何输入错误消息?
public function importExcel()
{
if (empty(Input::file('import_file')->getRealPath())) {
return back()->with('success','No file selected');
}
else {
$path = Input::file('import_file')->getRealPath();
$inserts = [];
Excel::load($path,function($reader) use (&$inserts)
{
foreach ($reader->toArray() as $rows){
foreach($rows as $row){
$inserts[] = ['biometrics' => $row['biometrics'], 'first_name' => $row['first_name'], 'last_name' => $row['last_name'], 'date' => $row['date'], 'emp_in' => $row['emp_in'], 'emp_out' => $row['emp_out']];
}
}
});
if (!empty($inserts)) {
DB::table('attendances')->insert($inserts);
return back()->with('success','Inserted Record successfully');
}
return back();
}
}
您可以像下面这样设置错误信息
return redirect()->back()->with('errors', 'No file selected');
然后在 blade 中显示错误消息,就像在 laravel 文档中提到的那样。 它在 session 中存储消息,您可以轻松地在 blade 文件中显示会话消息,如下所示
@if (count($errors) > 0)
<!-- Form Error List -->
<div class="alert alert-danger error">
<strong>Whoops! Something went wrong!</strong>
<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
您可以在调用 getRealPath() 函数之前放置一个 if
if( Input::file('import_file') ) {
$path = Input::file('import_file')->getRealPath();
} else {
return back()->withErrors(...);
}