🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to Upload files or Documents in Mysql Database Using laravel.

Step: 1- First of all you have to go:-

Step:2- then right-click in this Drive and got to Git Bas Here and click.

Step:3-run the composer

step: 4- write the project name. then press Enter. it will take time for this run this project


Step:5- The next step go to inside the project folder and click the Git base Here and write php artisan serve . and Enter

And copy the url like that: http://127.0.0.1:8000/ run this url in the web browser.

Step:6- see the pic

Step:7- now we need to create a File model.

Step:8- migrations using the following command.

Step:9– Create the file table.

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('files', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->string('title');
$table->text('overview');
$table->decimal('price', 6, 2);
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('files');
}
}

Step:9-  File.php, write the following code.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class File extends Model
{
use SoftDeletes;
protected $fillable = [
'title',
'overview',
'price'
];
public function user()
{
return $this->belongsTo(User::class);
}
}
view raw file.php hosted with ❤ by GitHub

Step:10- Now, define the Relationship inside the User.php model.

<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function files()
{
return $this->hasMany(File::class);
}
}
view raw User.php hosted with ❤ by GitHub

Step:11- php artisan make:controller FileController. run this command in composer.

Step:12- Create a View and Route for uploading files.

Route::post('upload', 'FileController@upload')->name('upload');
view raw web.php hosted with ❤ by GitHub

Step:13- home.blade.php create this page

@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">File Upload</div>
<div class="card-body">
<form method="POST" action="{{ route('file.upload') }}" aria-label="{{ __('Upload') }}">
@csrf
<div class="form-group row">
<label for="title" class="col-sm-4 col-form-label text-md-right">{{ __('Title') }}</label>
<div class="col-md-6">
<input id="title" type="text" class="form-control{{ $errors->has('title') ? ' is-invalid' : '' }}" name="title" value="{{ old('title') }}" required autofocus />
@if ($errors->has('title'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('title') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="overview" class="col-sm-4 col-form-label text-md-right">{{ __('Overview') }}</label>
<div class="col-md-6">
<textarea id="overview" cols="10" rows="10" class="form-control{{ $errors->has('overview') ? ' is-invalid' : '' }}" name="overview" value="{{ old('overview') }}" required autofocus></textarea>
@if ($errors->has('overview'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('overview') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="price" class="col-md-4 col-form-label text-md-right">{{ __('Price') }}</label>
<div class="col-md-6">
<input id="price" type="text" class="form-control{{ $errors->has('price') ? ' is-invalid' : '' }}" name="price" required>
@if ($errors->has('price'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('price') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Upload') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection

Step:14- you need to define the Post route inside routes >> web.php file.

<?php
// web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::post('file/upload', 'FileController@store')->name('file.upload');
view raw web.php hosted with ❤ by GitHub

you can see the page view.

Step:15- Storing the form data.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FileController extends Controller
{
public function store(Request $request)
{
$request->validate([
'title' => 'required:max:255',
'overview' => 'required',
'price' => 'required|numeric'
]);
auth()->user()->files()->create([
'title' => $request->get('title'),
'overview' => $request->get('overview'),
'price' => $request->get('price')
]);
return back()->with('message', 'Your file is submitted Successfully');
}
}

Also, we need to update our view to see the flash message.

@if(session()->get('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
@endif

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.