Laravel Tutorial
Home
Laravel Installation
Laravel Routing
Laravel Middleware
Laravel CSRF Protection
Laravel Response
Laravel Controllers
Laravel Hello world
Laravel Views
Laravel Session
Laravel Example
Laravel Form Validation
Laravel File Uploading
Laravel Insert Data
Laravel Display Data
Laravel Delete Data
Laravel Update Data
Laravel Send Mail
For creating table the SQL query is
CREATE TABLE demo ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `email` varchar(30) NOT NULL, `password` varchar(30) NOT NULL, PRIMARY KEY (id) );
Create Routes
routes/web.php : In next step, we will add new two routes in web.php file.
Route::get('/', function () { return view('registration'); }); Route::post('crud_insert', 'CrudController@save');
Creating The CrudController
Create a controller file CrudController.php and save it in the following path app/Http/Controllers/CrudController.php.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\CrudModel; class CrudController extends Controller { function save(Request $req){ $data = new CrudModel; $data->name = $req->name; $data->email = $req->email; $data->password = $req->pwd; $data->save(); return back() ->with("status","Insert Successfully."); } } ?>
Creating The CrudModel
Create a controller file CrudModel.php and save it in the following path app/CrudModel.php.
<?php namespace App; use Illuminate\Database\Eloquent\Model; class CrudModel extends Model { protected $table = 'demo'; public $timestamps = false; } ?>
Creating The View
Create a view file registration.blade.php and save the below code it in resources/views/registration.blade.php.
<!DOCTYPE html> <html lang="en"> <head> <title>Registration Form</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> </head> <body> <div class="container mt-5"> <h1 class="text-center">Registration Form</h1> <form action="crud_insert" method="post"> @csrf @if ($message = Session::get('status')) <div class="alert alert-success alert-block"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> @endif <label for="name" class="mb-2 mr-sm-2">Name:</label> <input type="text" class="form-control mb-2 mr-sm-2" id="name" placeholder="Enter name" name="name" > <label for="email2" class="mb-2 mr-sm-2">Email:</label> <input type="text" class="form-control mb-2 mr-sm-2" id="email2" placeholder="Enter email" name="email" > <label for="pwd2" class="mb-2 mr-sm-2">Password:</label> <input type="text" class="form-control mb-2 mr-sm-2" id="pwd2" placeholder="Enter password" name="pwd" > <input type="submit" class="btn btn-primary mb-2" value="Insert"> </form> </div> </body> </html>
Let us execute this example by the following URL in the browser. This URL may be different based on your website.
https://howtowebcode.com/