In this exercise we are going to create a way to add students using a form.
##Step 1
Create a new file called new_student.blade.php in your views folder. That is where you will store your form.
Type in the html code on the form
<!DOCTYPE html>
<html>
<body>
<form>
<input type="text" name="name"/>
<input type="text" name="email"/>
<input type="text" name="course"/>
<input type="hidden" name="_token" value = "{{csrf_token()}}"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
This is just a normal form the extra value _token is a security feature provided by Laravel to protect your app against a CSRF attack.
##Step 2
With our form ready we can now display it by adding a route to the form on the routes.php file
Route::get('/students/add', function () {
return view('new_student');
});
Now run the project. You should see the empty form on the page
##Step 3
Let us now add labels so that entry items can be identified.
<!DOCTYPE html>
<html>
<body>
<form>
<label for="name">Name</label>
<input type="text" name="name"/>
<label for="email">Email</label>
<input type="text" name="email"/>
<label for="course">Course</label>
<input type="text" name="course"/>
<input type="hidden" name="_token" value = "{{csrf_token()}}"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
##Step 4
Now we need to make our form active. To do that we need a route to post the data to. We then need to add that route to the form.
On the form element add the line
<form method="post" action="students/add">
So that now the entire form looks like this
<form method="post" action="/students/add">
<label for="name">Name</label>
<input type="text" name="name"/>
<label for="email">Email</label>
<input type="text" name="email"/>
<label for="course">Course</label>
<input type="text" name="course"/>
<input type="hidden" name="_token" value = "{{csrf_token()}}"/>
<input type="submit" value="Submit"/>
</form>
We then need to add that route to the routes file. Just below the previous entry now add
Route::post('/students/add', function () {
return "student added";
});
Now reload the form and submit.
You should see on the screen "Student added".
##Step 5
You can now confirm that data is actually being sent by mirroring back the post data.
Route::post('/students/add', function () {
return Request::all();
});
This new code should replace the previous post item.
You should now see the data that you post.
##Step 6
With all the data we can now save it and return to the all students link to see if they have been added.
Change the post route once more so that it now looks like this
Route::post('/students/add', function () {
//Create a new student object as we did in the tinker terminal
$student = new App\Student();
$student->name= Request::get('name');
$student->email= Request::get('email');
$student->course= Request::get('course');
$student->save();
//Once saved redirect back to all students table
return Redirect::to('/students');
});
Reload the student form, fill in the details and submit. You should be taken to the all students list with the new data added.
