ziben69's avatar

File Upload to database

Hello guys,

i am trying to do file upload system. When I click on the button, nothing happens. Can someone help me?

View's form:

<form action="{{ action ('BillController@store')}}" method="POST" enctype="multipart/form-data">
                    <input type="hidden" name="_token" value="{{csrf_token() }}" />
                    <div class="form-group">
                        <label>File:</label> <input type="file"
                            class="form-control" name="billcover" />
                    </div>
                    <input type="submit" value="Dodaj" class="btn btn-primary" />
                </form>

Controller's methods:

use Illuminate\Http\Request;
use App\Models\Bill;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
. . .

public function create()
    {
        return view('bills.create');
    }
      
    public function store(Request $request)
    {
        request()->validate([
            'name' => 'required',
        ]);
        $cover = $request->file('billcover');
        $extension = $cover->getClientOriginalExtension();
        Storage::disk('public')->put($cover->getFilename().'.'.$extension,  File::get($cover));
        
        $bill = new Bill();
        $bill->name = $request->name;
        $bill->mime = $cover->getClientMimeType();
        $bill->original_filename = $cover->getClientOriginalName();
        $bill->filename = $cover->getFilename().'.'.$extension;
        $bill->save();
        
        return redirect()->route('bills.index')
        ->with('success','Bill added successfully...');
    }
. . .

Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Bill extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'filename', 'mime', 'original_filename',
    ];
    
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

Routing:

Route::get('bills/create', 'BillController@create');
Route::post('bills/', 'BillController@store');

and filesystems.php:

 'public' => [
            'driver' => 'local',
            'root'   => public_path() . '/uploads',
            'url' => env('APP_URL').'/public',
            'visibility' => 'public',
        ],

It should store files in database Bill table, and in directory "public/uploads", nothing there after click on Submit. Null of errors. After clicked on button just page refresh.

0 likes
2 replies
m7vm7v's avatar
m7vm7v
Best Answer
Level 51

When you validate you use 'name' when the name is 'billcover'

    request()->validate([
            'billcover' => 'required',
        ]);

You could place this in the form before the label to have a bit more visibility

                @if ($errors->has('billcover'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('billcover') }}</strong>
                                    </span>
                                @endif
2 likes
ziben69's avatar

Thanks for really quick answer. It help :D :* and it WORKS ! :)

Please or to participate in this conversation.