[英]How to make Laravel controller store file upload with a relationship
我正在嘗試存儲與Employee模型相關的上傳文件。 上傳文件以將其作為外鍵保存到數據庫表后,我無法檢索員工ID。 路線:
Route::resource('employees', 'EmployeesController');
Route::post('documents', 'DocumentsController@createdocument')
因此,當我單擊上載時,我使用的URL上顯示http://localhost/public/employees/8
,它將重定向到http://localhost/public/documents
,文件確實上載,但在寫入數據庫時顯示錯誤。
這是我的代碼。 我該怎么做?
public function createdocument(Request $request, Employee $id)
{
$file = $request->file('file');
$allowedFileTypes = config('app.allowedFileTypes');
$maxFileSize = config('app.maxFileSize');
$rules = [
'file' => 'required|mimes:'.$allowedFileTypes.'|max:'.$maxFileSize
];
$this->validate($request, $rules);
$time = time(); // Generates a random string of 20 characters
$filename = ($time.'_'.($file->getClientOriginalName())); // Prepend the filename with
$destinationPath = config('app.fileDestinationPath').'/'.$filename;
$uploaded = Storage::put($destinationPath, file_get_contents($file->getRealPath()));
if($uploaded){
$employee = Employee::find($id);
$empdoc = new EmpDocuments();
$empdoc->name = $filename;
$empdoc->employee_id = $employee->id;
$empdoc->save();
}
return redirect('employees');
}
這些是我的模特。
Employee.php
public function EmpDocuments()
{
return $this->hasMany('App\EmpDocuments');
}
public function createdocument(){
return $this->EmpDocuments()->create([
'name' => $filename,
'employee_id' => $id,
]);
}
EmpDocuments.php
public function Employee()
{
return $this->belongsTo('App\Employee');
}
使用上述模型和控制器,我現在遇到錯誤
常規錯誤:1364字段'employee_id'沒有默認值(SQL:插入empdocuments。
如何捕獲employee_id?
設法解決它,以防有人遇到類似問題。 確保您將id與route操作一起傳遞,以便在下一個請求中將其捕獲。 這是最終的控制器。
public function update(Request $request, $id)
{
$file = $request->file('file');
$allowedFileTypes = config('app.allowedFileTypes');
$maxFileSize = config('app.maxFileSize');
$rules = [
'file' => 'required|mimes:'.$allowedFileTypes.'|max:'.$maxFileSize
];
$this->validate($request, $rules);
$time = time(); // Generates a random string of 20 characters
$filename = ($time.'_'.($file->getClientOriginalName())); // Prepend the filename with
$destinationPath = config('app.fileDestinationPath').'/'.$filename;
$uploaded = Storage::put($destinationPath, file_get_contents($file->getRealPath()));
if($uploaded){
$employee = Employee::find($id);
$empdoc = new EmpDocuments();
$empdoc->name = $filename;
$employee->empdocuments()->save($empdoc);
return redirect('employees/' . $id . '#documents')->with('message','Document has been uploaded');
}
}
Employee和EmpDocuments之間有關系嗎? 如果我對EmpDocuments屬於TO員工的理解很好,對嗎?
我正在嘗試提供幫助,但我需要了解,一位員工可以擁有許多文件嗎? 但是每個文檔只屬於一個雇員嗎?
如果這樣,您應該在員工模型中建立關系,
` public function employeeDocuments(){
return $this->hasMany(EmpDocuments::class);
}`
然后在同一模型中
`public function createEmployeeDocuments(){
return $this->employeeDocuments()->create([
'your_db_fields' =>your file,
'your_db_fields' => your other some data,
]);
}`
ID將自動插入,希望對您有所幫助!! https://laravel.com/docs/5.3/eloquent-relationships
您可填寫的是空的嗎??? 要使用雄辯的創建方法,您需要將可填充數組設置為批量分配。 嘗試此操作,如果仍然無法運行,請告訴我,我將盡力而為。
protected $fillable = [ 'employee_id', 'Your_db_field', 'Your_db_field', 'per_page', 'Your_db_field', 'Your_db_field' ];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.