简体   繁体   中英

How can you insert a file into a database using Eloquent in Laravel framework?

Am trying to insert image into a database in my project but things ain't working as expected, Am not sure as to where am getting it wrong. i keep getting this error message from my alert template.

在此处输入图像描述

The image must be an image. The image must be a file of type: jpeg,png, jpg, gif, svg.

mean while the uploaded image is an image with Jpeg format. i need help

public function register(Request $request){

    $this->validate($request, [
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    $newStudent = new Student;

     if($file = $request->hasFile('image')) {
        $file = $request->file('image') ;
        $fileName = $file->getClientOriginalName() ;
        $destinationPath = public_path().'/uploads/' ;
        $file->move($destinationPath,$fileName);
        $newStudent->image = '/public/uploads/'.$fileName ;
    }
        $newStudent -> StudentID = $request -> input('StudentID');
        $newStudent -> cLevel = $request -> input('cLevel');
        $newStudent -> surName = $request -> input('surName');
        $newStudent -> fName = $request -> input('fName');
        $newStudent -> fEmail = $request -> input('fEmail');
        $newStudent -> DoB = $request -> input('DoB');
        $newStudent -> pAddress = $request -> input('pAddress');


    $newStudent -> save();
   return response()->json(['status' => 'Image Uploaded Successfully']);
}

Please add line in your <form... enctype="multipart/form-data"> tag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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