Insert image with its original name in Database using Laravel

HTML Form in view.

<form method="post" action="{{route('article.store')}}" enctype="multipart/form-data">
    <div class="form-group">
      
      {{ csrf_field() }}

     
      </div>
      <div class="form-group">
      <label for="Title of Article"> Title of Article</label>
      <input type="text" name="title" class="form-control" >
      </div>
      <div class="form-group">
        <label for="">Header Image</label>
        <input type="file" name="image" id="" class="form-control">
      </div>
      
      
    <div class="form-group">
      <label>Description of Article</label>
      <textarea name="desc" class="form-control"></textarea>
      </div>
     
      
      <input type="submit" value="Submit">
      
    
  </form>

Controller to store image in file and database.

$article is an object of Model and $request is request from form.

public function store(Request $request)
    {
        $request->image->move(public_path('upload'), $request->file('image')->getClientOriginalName());
        $path = $request->file('image')->getClientOriginalName();
        $article = new Article;
        $article->title =$request->title;
        $article->image = $path;
        $article->desc = $request->desc;
        if($article->save()){
            return "Data inserted Successfully";
        }

Leave a Reply

Your email address will not be published. Required fields are marked *