This will undo all your migrations.
When you generate a model and at the same time you want to generate a database migration, you can either use the –migration or -m option:
php artisan make:model Manager --migration
php artisan make:model Manager -m
Seeders
Many get confused and think its too complex to create for seeders but it is simply a class that populates your database. The good thing with seeders is that they can be executed using a simple command to refresh your database. It helps create proper data in newly created database instead of sample data.
The basic idea behind seeders is to help the problem of “dirty data” where one can develop a simple or even a powerful seeder. Overall seeders are a special set of classes that allow us to populate our database over and over with the same exact data. Let’s implement the following command:
php artisan make:seeder ManagersRecordSeeder
In the text editor, under the seeds folder, open the newly created file with filename: ManagersRecordSeeder.php. As you can see, this is just a very simple class with a single method called run().
<?php
use IlluminateDatabaseSeeder;
class ManagersRecordSeeder extends Seeder
{
/**
* Run the database seeds
* @return void
*/
public function run()
{
//
}
}
The code is just a wrapper around a Console Command class, made specifically to help with the seeding task. Modify the code and then save it.
public function run()
{
echo 'Seeding!';
}
And, will go back to the terminal:
php artisan db:seed --class=ManagersRecordSeeder
This is just purely calling the DB facade, but keep in mind that there’s no actual database interaction here. You can now populate the table with a few entries and run:
php artisan db:seed --class=class=ManagersRecordSeeder
Here you can keep deleting, adding, editing entries while you work, and then reset them with a simple command.
CRUD with Eloquent
CRUD operations under the Eloquent object-relational mapper (ORM) make it easier for Laravel developers to work with multiple databases. It performs create, retrieve, update, and delete (CRUD) operations, as well as maps object models to database tables. It handles all the database interaction required for CRUD operations.
Creating records
You can use the ::create method to insert a new record in the database.
manager_record::create(array(
'first_name' => 'Michel',
'last_name' => 'Hokings',
'manager_rank' => 1
));
Aside from the simple create method shown above, you can also create a new object and assign different attributes to it. Then, you can call the save() function and execute the code. Methods such as firstOrCreate() or firstOrNew() are other options for creating records. These will enable finding a student with certain attributes; if that student is not found, then you will either create it in the database or instantiate a new instance.
Retrieving records
Using Eloquent ORM, getting and finding records from the database is manageable and easy. The queries are simply built and offer a smooth flow. For creating ::where statements, you will use get() and first() methods. The first() method will return only one record, while the get() method will return an array of records that you can loop over. Also the find() method can be used with an array of primary keys, which will return a collection of matching records. Here are some few examples:
$manaer = Managers::all();
This code gets all the managers. While the code below, finds a specific manager by id:
$manager = Managers::find(1);
Also, as shown below, the code describes to find a manager based on specific attribute.
$michel = Managers::where('name', '=', 'Michel')->first();
For the get() method, this code shows how to find a manager with a rank level greater than 5.
$rankManagers = Managers::where('manager_rank', '>', 5)->get();
Updating records
Updating records using Eloquent is as easy. To update a record, just find the record you would like to update, change the attributes, and save. For example, to change the manager rank level of John Doe to 5, first find the manager and then execute the save method.
$MichelHokings = Bear::where('name', '=', 'Michel')->first();
$MichelHokings->danger_level = 5;
$MichelHokings->save();
The save method may also be used to update models that already exist in the database.
Deleting records
Eloquent boasts its easy process of updating records, but it has the same story with deleting. There are two options: record pull-out and execute delete method, or simply use the destroy method. To find and delete a record, simply execute the following commands:
$manager = Managers::find(1);
$manager->delete();
To delete a record and multiple records, the commands are executed:
Managers::destroy(1);
Managers::destroy(1, 2, 3);
Please note that the parameters of destroy are primary keys only unlike the delete method which can accept any database column.
To find and delete all managers with rank level that is greater than 10.
Managers::where('manager_rank', '>', 10)->delete();
Writing a web application in PHP, developers have the option to choose from a rich list of PHP frameworks. The ongoing demand combined with several usage statistics posted from communities suggest that Laravel is currently more popular than other PHP frameworks. However, seasoned web developers never choose a PHP framework based on its popularity or hype. There are pros and cons to consider. A lot of developers tend to downplay the popularity of PHP, but keep in mind that you must choose a PHP framework that fits all the project requirements. Also as a developer, it is important to use a PHP framework, as Laravel helps you reduce web application development cost.
The robust features and tools provided by Eloquent inside Laravel make it easier for developers to build custom web applications following specified business requirements. This article has given you a primer on how to use the basic features of Eloquent ORM. Keep in mind that some features often impact the performance of any Laravel application. Hence you have to implement a number of performance optimization techniques to boost the application’s speed and user experience. But Laravel has been evolving consistently to meet emerging web application development trends. You can always accelerate custom web application development by using the new features and enhancements included in the latest version of Laravel. Learn more about Eloquent and Laravel through its detailed documentation.
In a very competitive world of custom web applications, the health and well-being of the application are synonymous with business. This article discussed how Eloquent efficiently provide an effortless way to communicate with a database. The SQL queries from your application to the database are very critical. For example, a company may have a standard concerning the SQL query execution time. If a query is above 50 ms and according to their standard that is considered a slow query then a developer needs to perform optimization of the query as soon as possible. Time is very critical in this scenario, so a developer needs help in finding where and what causes the slow query. Thus Retrace can truly help developers by answering the where and what questions.
Slow queries can be tracked in milliseconds under the Took (ms) column and each duration is accompanied by the Web Requests. Thus when a slow query is detected, its corresponding attributes are directly pinpointed.
Conclusion
PHP is a powerful language. Laravel is considered a famous and robust PHP framework. Laravel with Eloquent provides competitive technology for web applications. Also Retrace is a powerful tool for tracing performance of applications built in PHP. It provides an efficient way of tracking SQL queries. Not just tracking, but providing information on specific queries provides a lot more help to developers.