Skip to content

Commit ee9e2cb

Browse files
committed
data insert done in Laravel way using Eloquent | web routes added
1 parent ba5d746 commit ee9e2cb

File tree

5 files changed

+107
-11
lines changed

5 files changed

+107
-11
lines changed

app/Http/Controllers/MainController.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,33 @@
55
use Illuminate\Http\Request;
66
use App\Http\Requests;
77

8+
//using model
9+
use App\Employee;
10+
811
class MainController extends Controller
912
{
1013
public function index(){
11-
header("Location: www.google.com");
14+
return "<h1>Laravel with Angular 2 REST</h1>";
15+
}
16+
17+
//form using laravel 5.3
18+
public function create(){
19+
$allEmployee = Employee::all()->sortByDesc('id'); //order by id DESC
20+
return view('pages/createForm',compact('allEmployee'));
21+
}
22+
23+
//store data after form submission
24+
public function store(Request $request){
25+
26+
$employee = new Employee;
27+
$employee->firstname = $request->fname;
28+
$employee->lastname = $request->lname;
29+
$employee->DOB = $request->dob;
30+
$employee->description = $request->desc;
31+
32+
//save data
33+
$employee->save();
34+
//return back
35+
return redirect()->to('/create');
1236
}
1337
}

public/views/layout.blade.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Laravel with Angular 2</title>
6+
<!-- Latest compiled and minified CSS -->
7+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
8+
</head>
9+
<body>
10+
11+
<div class="container">
12+
<h1>This is Laravel</h1>
13+
@yield('content')
14+
</div>
15+
</body>
16+
</html>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
@extends('layout') @section('content')
2+
3+
<h2>Create New Employee</h2>
4+
<br>
5+
<br>
6+
<hr>
7+
<div class="col-md-6">
8+
9+
<!--form to create new employee -->
10+
<form class="form-horizontal" method="POST" action="/employee/store">
11+
<div class="form-group">
12+
<label for="fname" class="col-sm-2 control-label">Firstname</label>
13+
<div class="col-sm-10">
14+
<input type="text" class="form-control" id="fname" name="fname" placeholder="firstname" required>
15+
</div>
16+
</div>
17+
18+
<div class="form-group">
19+
<label for="lname" class="col-sm-2 control-label">Lastname</label>
20+
<div class="col-sm-10">
21+
<input type="text" class="form-control" id="lname" name="lname" placeholder="lastname" required>
22+
</div>
23+
</div>
24+
25+
<div class="form-group">
26+
<label for="dob" class="col-sm-2 control-label">DOB</label>
27+
<div class="col-sm-10">
28+
<input type="date" class="form-control" name="dob" id="dob">
29+
</div>
30+
</div>
31+
32+
<div class="form-group">
33+
<label for="desc" class="col-sm-2 control-label">Description</label>
34+
<div class="col-sm-10">
35+
<textarea name="desc" rows="5" cols="42" id="desc" name="desc"></textarea>
36+
</div>
37+
</div>
38+
39+
<button style="float: right;" type="submit" class="btn btn-primary">Submit</button>
40+
</form>
41+
</div>
42+
43+
<div class="col-md-6">
44+
<h3>All Employees</h3>
45+
@foreach($allEmployee as $employee)
46+
<div class="panel panel-info">
47+
<div class="panel-heading">
48+
<h3 class="panel-title">{{$employee->firstname}} {{$employee->lastname}}</h3>
49+
</div>
50+
<div class="panel-body">
51+
<li><em>ID: </em> {{$employee->id}}</li>
52+
<li><em>DOB: </em> {{$employee->DOB}}</li>
53+
<li><em>Description</em> {{$employee->description}}</li>
54+
</div>
55+
</div>
56+
@endforeach
57+
</div>
58+
59+
@stop

public/views/pages/index.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

routes/web.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,12 @@
1414
//root page
1515
Route::get('/','MainController@index');
1616

17+
//route for showing form
18+
Route::get('/create','MainController@create');
19+
20+
//store data after form submission
21+
Route::post('/employee/store','MainController@store');
22+
23+
1724
//test route
1825
//Route::get('/delete/id={id}','EmployeeController@delete');

0 commit comments

Comments
 (0)