How to get data form database using Laravel 6
In this example, we are going to show you how to insert data in the database using laravel framework PHP.
For insert data in MySQL using laravel first, we have to create a table in the data base.
The INSERT INTO statement is used to insert new data to a MySQL table:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
To learn more about SQL, please visit our
For creating table the SQL query is:
SQL Query
CREATE TABLE student_details
(
id int NOT NULL AUTO_INCREMENT,
first_name varchar(50),
last_name varchar(50),
city_name varchar(50),
email varchar(50),
PRIMARY KEY (id)
);
Create 3 files for insert data in Laravel :
- StudInsertController.php (app/Http/Controllers/StudInsertController.php)
- ViewController.php(app/Http/Controllers/ViewController.php)
- stud_create.php (resources/views/stud_create.php)
- views.blade.php (resources/views/views.blade.php)
- web.php (routes/web.php)
stud_create.php
<!DOCTYPE html>
<html>
<head>
<title>Student Management | Add</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.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.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.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<br>
<h2 class="text-center">Form Insert Data Student</h2>
<form action = "/create" method = "post">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<div class="form-group">
<div class="row">
<div class="col-6">
<label for="usr">First Name:</label>
<input type='text'class="form-control" name='first_name' required >
</div>
<div class="col-6">
<label for="usr">Last Name:</label>
<input type="text" class="form-control" name='last_name' required>
</div>
</div>
</div>
<div class="form-group">
<label for="pwd">City Name:</label>
<select class="form-control" name="city_name" required>
<option >Please select city</option>
<option value="Phnom Penh">Phnom Penh</option>
<option value="USA">USA</option>
<option value="English">English</option>
<option value="Canada">Canada</option>
<option value="Frence">Frence</option>
</select>
</div>
<div class="form-group">
<label for="pwd">City Name:</label>
<input type="text" class="form-control" name='email' required>
</div>
<input type ='submit' class=" btn btn-success text-center" value = "Insert">
</div>
</form>
</body>
</html>
views.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>View Report</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css">
<!-- library bootstrap -->
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js"></script>
</head>
<body>
<!-- table user all -->
<table id="tableHorizontalWrapper" class="table table-striped table-bordered table-sm text-center" cellspacing="0"width="50%">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>City Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach($viewReport as $views)
<tr>
<td>{{$views->id}}</td>
<td>{{$views->first_name}}</td>
<td>{{$views->last_name}}</td>
<td>{{$views->city_name }}</td>
<td>{{$views->email }}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
StudInsertController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StudInsertController extends Controller {
public function insertform(){
return view('stud_create');
}
public function insert(Request $request){
$first_name = $request->input('first_name');
$last_name = $request->input('last_name');
$city_name = $request->input('city_name');
$email = $request->input('email');
$data=array('first_name'=>$first_name,"last_name"=>$last_name,"city_name"=>$city_name,"email"=>$email);
DB::table('student')->insert($data);
echo "Record inserted successfully.<br/>";
echo '<a href = "/insert">Click Here</a> to go back.';
}
}
ViewController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class ViewController extends Controller
{
public function view()
{
$riewRow = DB::select('SELECT * FROM tbl_insert');
return view('views',['viewReport'=> $riewRow]);
}
}
web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//insert dataRoute::get('insert','StudInsertController@insertform');
Route::post('create','StudInsertController@insert');
Route::get('view','ViewController@view');
0 Comments
CAN FEEDBACK
Emoji