To insert information right into a MySQL database utilizing Laravel 11, it’s important to observe the beneath steps:
Minimal requirement: PHP 8.2 or increased is required to run Laravel 11.
Step 1: Create a database
create databse userdb
Step 2: Create the MySQL desk (tblusers)
Desk construction for desk tblusers
CREATE TABLE `tblusers` ( `id` int(11) NOT NULL, `firstName` varchar(255) DEFAULT NULL, `lastName` varchar(255) DEFAULT NULL, `emailId` varchar(255) DEFAULT NULL, `mobileNumber` bigint(11) DEFAULT NULL, `handle` varchar(255) DEFAULT NULL, `state` varchar(255) DEFAULT NULL, `metropolis` varchar(255) DEFAULT NULL, `postingDate` timestamp NULL DEFAULT current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
Step 3: Set Up Your Surroundings
Ensure your MySQL database is correctly configured. In your .env file, configure the database connection:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_database_username DB_PASSWORD=your_database_password
Step 4: Create a blade file (sources/views/kind.blade.php)
<html> <head> <title></title> <hyperlink href="https://cdn.jsdelivr.web/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.web/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script> <fashion> physique { background: #fafbfb; } /* FOOTER STYLES –––––––––––––––––––––––––––––––––––––––––––––––––– */ .page-footer { place: mounted; proper: 0; backside: 50px; show: flex; align-items: middle; padding: 5px; z-index: 1; } .page-footer a { show: flex; margin-left: 4px; }</fashion> </head> <physique> <div class="container my-5"> <div class="row justify-content-center"> <div class="col-lg-9"> <h1 class="mb-3">Register with us</h1> <hr /> @if(session('success')) <div class="alert alert-success">{{ session('success') }}</div> @endif @if(session('error')) <div class="alert alert-danger">{{ session('error') }}</div> @endif <kind technique="put up" motion="{{ route('insertdata') }}"> @csrf <div class="row g-3"> <div class="col-md-6"> <label for="your-name" class="form-label">First Title</label> <enter sort="textual content" class="form-control" id="firstName" title="firstName" worth="{{ outdated('firstName') }}"> <span class="text-danger"> @error('firstName') {{ $message }} @enderror </span> </div> <div class="col-md-6"> <label for="your-surname" class="form-label">Final Title</label> <enter sort="textual content" class="form-control" id="lastName" title="lastName" worth="{{ outdated('lastName') }}"> <span class="text-danger"> @error('lastName') {{ $message }} @enderror </span> </div> <div class="col-md-6"> <label for="your-email" class="form-label"> Electronic mail ID</label> <enter sort="e mail" class="form-control" id="emailId" title="emailId" worth="{{ outdated('emailId') }}"> <span class="text-danger"> @error('emailId') {{ $message }} @enderror </span> </div> <div class="col-md-6"> <label for="your-subject" class="form-label">Cellular No</label> <enter sort="textual content" class="form-control" id="mobileNumber" title="mobileNumber" worth="{{ outdated('mobileNumber') }}"> <span class="text-danger"> @error('mobileNumber') {{ $message }} @enderror </span> </div> <div class="col-12"> <label for="your-message" class="form-label">Handle</label> <textarea class="form-control" id="handle" title="handle" rows="5" > {{ outdated('handle') }}</textarea> <span class="text-danger"> @error('handle') {{ $message }} @enderror </span> </div> <div class="col-md-6"> <label for="your-email" class="form-label"> Metropolis</label> <enter sort="textual content" class="form-control" id="metropolis" title="metropolis" worth="{{ outdated('metropolis') }}"> <span class="text-danger"> @error('metropolis') {{ $message }} @enderror </span> </div> <div class="col-md-6"> <label for="your-subject" class="form-label">State</label> <enter sort="textual content" class="form-control" id="state" title="state" worth="{{ outdated('state') }}"> <span class="text-danger"> @error('state') {{ $message }} @enderror </span> </div> <div class="col-12"> <div class="row"> <div class="col-md-6"> <button sort="submit" class="btn btn-dark w-100 fw-bold" title="submit" >Submit</button> </div> </div> </div> </div> </kind> </div> </div> </div> </physique> </html>
Step 5: Create a Mannequin and Migration
Create a mannequin that can correspond to the database desk you wish to insert information into. You are able to do this utilizing the artisan command:
php artisan make:mannequin your_ModelName //for this tutorial php artisan make:mannequin UserInsert
You outline the desk title and fields which might be mass assignable (e.g., firstname
, LastName, emailId
) on this mannequin.
<?php namespace AppModelsmodel; use IlluminateDatabaseEloquentModel; class UserInsert extends Mannequin { protected $desk="tblusers"; public $timestamps = false; protected $fillable = [ 'firstName', 'lastName','emailId', 'mobileNumber','handle','state','metropolis' ]; }
Step 6: Create a controller for dealing with consumer inputs and insert the info into the database.
php artisan make:controller your_contollername //For this tutorial php artisan make:controller insertController
<?php namespace AppHttpControllers; use IlluminateSupportFacadesDB; use IlluminateHttpRequest; Use AppModelsUserInsert; class insertController extends Controller { public perform insertdata(Request $request){ $request->validate([ 'firstName' => 'required', 'lastName' => 'required', 'emailId' => 'required', 'mobileNumber' => 'required|numeric|digits:10', 'handle' => 'required', 'state' => 'required', 'metropolis' => 'required' ]); $consumer=DB::desk('tblusers') ->insert([ 'firstName' => $request->firstName, 'lastName' => $request->lastName, 'emailId' => $request->emailId, 'mobileNumber' => $request->mobileNumber, 'handle' => $request->handle, 'state' => $request->state, 'metropolis' => $request->metropolis ]); if($consumer){ return redirect()->route('kind')->with('success', 'Information inserted efficiently.'); } else{ return redirect()->route('kind')->with('error', 'One thing went unsuitable . Please Strive once more.'); } } }
Step7: Add a Path to Deal with the Type Submission
<?php use IlluminateSupportFacadesRoute; use AppHttpControllersinsertController; // For kind Route::get('/', perform () { return view('kind'); })->title('kind'); // For Type Submission Route::put up('/insertdata',[insertController::class,'insertdata'])->title('insertdata');
The way to run the Script
1. Obtain the venture zip file
2. Extract the file and duplicate insert-app
folder
3. Paste inside root listing (for xampp xampp/htdocs, for wamp wamp/www, for lamp var/www/Html)
4.Open PHPMyAdmin (http://localhost/phpmyadmin)
5. Create a database with the title userdb
6. Import userdb.sql
file(given contained in the zip package deal in SQL file folder)
7. Run these command
PS C :> cd xampp/htdocs/insert-app
PS C:xampphtdocsinsert-app> php artisan serve
8. After that open the browser run the script
The put up The way to insert information into MySQL database utilizing Laravel 11 appeared first on PHPGurukul.