Insert image with its original name in Database using Laravel

HTML Form in view.

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

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

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;
            return "Data inserted Successfully";

Leave a Reply

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