How to use Model in Laravel 7 | Laravel Eloquent for Laravel CRUD Operation
In this article we are going to learn how to use model in laravel . we will create a demo project and create a model and analyse how a model actually plays an important role in Laravel CRUD Operation .
Steps we will Follow :
- What is Model ?
- How to create Model
- Database creation and connection
- Configuring Model file
- Inserting data into the Database
- Create Controller
- Retriving data from Database
- Output
- Conclusion
What is a Model ?
Laravel works on MVC Framework , where the letter " M " stands for ' model ' .In Laravel , model is used to create , insert ,delete and retrive data from the database . A Model is basically connects with a table and helps in performing the CRUD operation . In Laravel all the models created are stored inside the ' App ' Folder . You should follow the following naming convention for creating a model as it is not compulsory but we should use it to make our project easier in future .
Naming Convention :
- Use first character of model name as capital ( E.g : Student )
- Use the name in singular form ( E.g : Student not Students )
Creating Model :
Use the following command to create model
php artisan make:model Model-name
Let's create a demo model for our project
php artisan make:model Student
Setting up Database :
Let's create a database named ' model ' and a table named ' student ' with column fields ' id ' , ' name ' and ' email ' .
After creating database and table we need to connect our database to our project . To perform this open your ' .env ' file and make the following configurations .
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=model DB_USERNAME=root DB_PASSWORD=
configuring model file ( Student.php ) :
Open your model file present inside " App " folder and provide the table name present inside the database ( database name : model ) and use " fillable " property . You can also disable default timestamp property of laravel by using " $timestamps " variable as shown below :
Student.php ( source code ) :
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Student extends Model { protected $table="student"; protected $fillable = [ 'name', 'email' ]; public $timestamps = false; }
Now our model is connected with our database table .
Inserting Data :
Let's Manually enter some data into the table .
Creating Controller :
We need to create a controller so that we can retrive data from database through our model .
Use the following command to create a controller .
php artisan make:controller controller-name
Let's create a controller for our project --
php artisan make:controller StudentController
Retriving Data from Database :
Finally to retrive data we need to add some few lines of code in our controller . The most important thing is don't forget to include the " model " inside the controller file . Let's configure our route file ( web.php ) and controller ( StudentController.php ) .
StudentController.php ( Source Code ) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Student; class StudentController extends Controller { public function index() { $student=Student::all(); return view('welcome')->with(['student'=>$student]); } } |
web.php ( Source Code ) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | 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! | */ Route::get('/','StudentController@index'); |
view file ( welcome.blade.php ) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet"> <!-- Styles --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script> </head> <body> <div class="header my-5"> <center> <h1>Getting data from database</h1> </center> </div> <div class="data-table my-3 container"> <table class="table table-dark"> <thead> <tr> <th scope="col">Id</th> <th scope="col">Name</th> <th scope="col">E-mail</th> </tr> </thead> <tbody> @foreach($student as $data) <tr> <th scope="row">{{ $data->id }}</th> <td>{{ $data->name }}</td> <td>{{ $data->email }}</td> </tr> @endforeach </tbody> </table> </div> </body> </html> |
Output :
Conclusion :
This article is just a demo that how can you work with Laravel Model . Laravel Eloquent provides a lots of features that makes our development easy . we can integrate other programming languages with laravel like you can integrate Vue JS with Laravel . You can also use a lots packages with Laravel like DomPDF for converting a blade file to PDF , To create interactive animated alert box you can also use SweetAlert package etc.Thank you for reading this article -:)
Hope it helped you --
Also Read :
Laravel pagination in DomPDFLaravel SweetAlert
Integrate Vue JS with Laravel
Laravel Multi-Auth Package
Laravel File Upload
Covid-19 tracker Project