Hello World, Welcome to my website. Today we will discuss about how we can add data in database in laravel. In the previous tutorial, we have discussed about how we can implement form validation in laravel.
In this tutorial, we will extends the previous article. In this article, we will learn how can connect database with laravel add how we can add data in database. I have devided this tutorial in following steps.
- Setup controller for adding data
- Show success message in add form
Set controller for adding data
In this step, we will modify the Student Controller’s submit_student method. Add the following code just before the redirect function.
// Add data in database $student=new Student; $student->name=$request->name; $student->email=$request->email; $student->phone=$request->phone; $student->address=$request->address; $student->gender=$request->gender; $student->save();
Also replace the redirect function line with following code.
return redirect('add-student')->with('success','Data has been added.');
Show Success Message in Add Form
- Edit the /resources/views/add-student.blade.php and add the following code just before the form tag.
@if(Session::has('success')) <p class="alert alert-success">{{ session('success') }}</p> @endif
In the next article, we will learn about update data in database in laravel.
I hope you are enjoying my articles. If you have any query the please add your query in comment section :).