繁体   English   中英

如何使Laravel控制器存储具有关系的文件上传

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM