Upload file in PHP Laravel | CRUD in PHP Laravel

In this article we will learn how to upload file in PHP Laravel framework.

Create form
enctype=”multipart/form-data” must in form attribute.

<form action="{{ route('About.store') }}" method="POST" enctype="multipart/form-data">
        @csrf

        <div class="row">
             <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Title:</strong>
                    <input type="text" name="Title" class="form-control" placeholder="Title">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Picture:</strong> 
                    <input type="file" name="Picture" class="form-control">
                </div>
            </div>
              
            <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>

    </form> 

Edit from

<form action="{{ route('About.update', $about->id) }}" method="POST" enctype="multipart/form-data">
        @csrf
        @method('PUT')

        <div class="row">
                    <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong> Title:</strong>
                    <input type="text" name="Title" class="form-control" placeholder="{{ $about-> Title}}"
                        value="{{ $about-> Title}}">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong> Picture:</strong>
                    <input type="file" name="Picture" class="form-control">
                    <img src="{{ URL::to('/') }}/uploads/{{ $about->Picture }}" class="img-thumbnail" width="75" />
                    <input type="hidden" name="hidePicture" value="{{ $about-> Picture}}">

                </div>
            </div>
            

            <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>

    </form>

In controller create method

    public function store(Request $request)
    {
 
        $file = $request->file('Picture');
      
        $file->move('uploads',$file->getClientOriginalName());  
        $request = $request->all();
        $request['Picture'] = $file->getClientOriginalName();
        About::create($request);


        return redirect()->route('About.index')
            ->with('success', 'About created successfully.');
    }

Update method

    public function update(Request $request, $id)
    {   
        $HidePicture = $request->hidePicture; 
        $file = $request->file('Picture'); 
        $request = $request->all();
        
        if($file != '')
        {
            $file->move('uploads',$file->getClientOriginalName());  
            
            $request['Picture'] = $file->getClientOriginalName();
        }
        else {
            $request['Picture'] = $HidePicture;
        }


        $about = About::find($id); 
        $about->update($request);

        return redirect()->route('About.index')->with('success', 'About updated successfully');
    }