當我保存,我會得到這個錯誤如何使用Laravel Framwork保存並顯示PDF,Excel和Word文件?
未定義的變量:文件
真的,我不知道如何來存儲文件,但我在谷歌查了一些博客,這是我的存儲功能:
public function store(Request $request)
{
$this->validate($request, $this->rules);
if($request->hasFile('file')){
$file = $request->file('file');
$path = $file->getRealPath();
$files = file_get_contents($path);
}
$input = $request->all();
$charge = $request->get('charge');
$date = $request->get('date');
$job = $request->get('job_id');
$id = \Auth::id();
$current_id = DB::table('charges')->max('invoice_no');
$data = array();
foreach ($charge as $key=>$value){
$data[] = [
'date' => $date,
'charge'=>$value,
'job_id'=>$job,
'user_id'=>$id,
'amount'=>$input['amount'][$key],
'category'=>$input['category'][$key],
'file'=>$files[$key], // here i will get error
'invoice_no' => $current_id + 1,
];
}
我正在創建我們的公司應用程序,所以我們要主持雲。我很困惑將這些文件存儲在我的數據庫或磁盤但我們必須訪問這些文件。而在我的節目視圖中,我想要顯示名稱這個文件,當我點擊該名稱時,我想要顯示那個特定的文件,這怎麼可能?
編輯 這是我的形式
{!! Form::model(new App\Charge, ['route' => ['charges.store'],'class'=>'form-horizontal','role'=>'form']) !!}
<div class="form-group">
{!! Form::label('date', 'DATE :',['class'=>'col-md-2 control-label']) !!}
<div class="col-md-7">
{!! Form::date('date',\Carbon\Carbon::now(),null,['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('job_id', 'JOB ID :',['class'=>'col-md-2 control-label']) !!}
<div class="col-md-7">
{!! Form::select('job_id',$job,null,['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('charges', 'CHARGES :',['class'=>'col-md-2 control-label']) !!}
<div class="col-md-10">
<div class="input_fields_wrap">
<button class="add_field_button" value="">Add More Charges</button>
<div>
<input type="text" name="charge[]">
<input type="text" name="category[]">
<input type="text" name="amount[]">
<input type="file" name="file[]">
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-2 col-md-offset-8">
<submitfiled>
{!! Form::submit(['class'=> 'btn btn-primary form-control']) !!}
</submitfiled>
</div>
</div>
{!! Form::close() !!}
可以通過添加'DD($文件)調試;'行後7和更新您的問題嗎? –
是同樣的錯誤,我使用$文件即時獲取錯誤,該行**未定義變量:文件** – Hamelraj
請提供用於發起請求的窗體的HTML標記。 – Bogdan