doudao9896
doudao9896
2017-10-25 00:37

将上传的图像存储到数据库laravel中

已采纳

I am having trouble uploading my image into database, everytime I try to upload it, the data send into the database is empty but my local disk is able to store the image and I don't know what to do. I think I might have done something wrong in the controller part

Controller:

public function store1(Request $request){

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


   if ($request->hasFile('file')) {
        $image = $request->file('file');
        $name = $image->getClientOriginalName();
        $size = $image->getClientSize();
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $name);

        $userImage = new UserImage;
        $userImage->name = $name;
        $userImage->size = $size;
        //dd($userImage);
        $userImage->save;
}

table that I wanted to upload to:

Schema::create('user_images', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('size');
        $table->integer('user_id');
        $table->timestamps();
});

enter image description here

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • duanpie2414 duanpie2414 4年前

    In your posted code, you forgot setting the data for user_id. As I can see in your schema, it is not nullable so it should have a data.

    Also, can you try putting () in your save like so :

     $userImage->save();
    
    点赞 评论 复制链接分享