简体   繁体   English

图像未存储在数据库Laravel 5.8中?

[英]Image is not storing in the database Laravel 5.8?

The image is not storing in the database 图像未存储在数据库中

I already tried looking at other possible solutions but nothing work I also tried with using guessextension 我已经尝试过寻找其他可能的解决方案,但是我也没有尝试使用guessextension

public function store(Request $request)
    {

        $student = new student();

        $this->validateRequest();

        $student->name = $request->name;
        $student->address = $request->address;

    if ($request->hasFile('image')) {
        $image = $request->file('image');
        $extension = $file->getClientOriginalExtension();
        $filename=time() .'.'. $extension;
        $file->move('uploads/student/',$filename);
        $student->image = $filename;                   
    }   
    else  {
            return $request;
            $student->image ='';
    } 

     $student->save();

    return redirect()->route('show')->with('response', 'Registered Successfully');
} 

Here is the form 这是表格

@extends('layouts.app')
@section('content')
<div class="container">
<form method="get" action="/student" enctype="multipart/form-data">
   {{csrf_field()}}

    <div class="form-group">
    <label for="email">Name:</label>
    <input type="text" class="form-control" name="name" id="email">    
  </div>

  <div class="form-group">
    <label for="pwd">Address:</label>
    <input type="text" class="form-control" name="address" id="email">
  </div>

  <div class="custom-file">
  <input type="file" name="image" class="custom-file-input" >
  <label class="custom-file-label" for="customFile">Choose file</label>
</div>

  <button type="submit" class="btn btn-primary">Submit</button>

</div>

@stop 

What I need is the image to be stored in the database. 我需要的是要存储在数据库中的图像。

actually, when you upload an image to the server you save only the path of the image into your database 实际上,当您将图像上传到服务器时,您仅将图像的路径保存到数据库中

so firstly you need to Create a symbolic link from "public/storage" to 所以首先您需要创建一个从"public/storage"

"storage/app/public"

by artisan command: php artisan storage:link 通过artisan命令: php artisan storage:link

after that, you can optimize this part of your code from this 之后,您可以从此处优化代码的这一部分

if ($request->hasFile('image')) {
    $image = $request->file('image');
    $extension = $file->getClientOriginalExtension();
    $filename=time() .'.'. $extension;
    $file->move('uploads/student/',$filename);
    $student->image = $filename;                   
}

to this simple one 这个简单的

if ($request->hasFile('image')) {
    $image = $request->image->store('uploads');                   
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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