mercredi 30 novembre 2016

How to store a relationship between a file and a model in Larave?

I have the below controller. I need to save the Document Model with the Employee_id relationship. I have the relationship set correctly but I a, having SQL error where it says employee_id cannot be null, foreign key violation.

So I think I have to find a way for the controller to recognise the currently loaded id for the employee profile view then save the document model. The upload works fine. It's writing to the DB part that I need help. Code here....

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();  
    $filename = ($time.'_'.($file->getClientOriginalName())); // Prepend the filename 
     $destinationPath = config('app.fileDestinationPath').'/'.$filename;
        $uploaded = Storage::put($destinationPath, file_get_contents($file->getRealPath()));
        if($uploaded){

            $employee = Employee::find($id); 

            EmpDocuments::create([
                'employee_id' => $employee,
                'name' => $filename
            ]);
        }    
        return redirect('employees');
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2gHKZDk
via IFTTT

Aucun commentaire:

Enregistrer un commentaire