The ORM which is included with Laravel offers a reliable and easy Active Record that is implemented on behalf of to work with your database. A corresponding model will be present in each database that can available to interact the following table. It allows you to query for the data in the table and as well as include new records into the table.
Before proceeding this you have to configure the connection in the database through database.php.
Defining the Models
Before starting we have to create an Eloquent Model. The app directory has the mmodel,you can also even relocate those models anywhere accordingly. Here we provide the most easiest way to create a model instance that’s to by the model artisan command.
php artisan make:model User
The class plural name will be used as the table name unless the name the was specifically mentioned .you can also metion a custom table by defining the property of the table in your model .
The Eloquent will assume itself the every table should have a column based on primary key named Id . A convention can override by defining a $ protected primary key. The default primary key will be assigned as the int automatically. If it was not an integer you can even set by yourself .
All the database connection configuration will be set as default for all the Eloquent models. If you would like to specify a various different connection please use connection property.
After creating database model you can be able to retrieve data from the database table. A query builder allows you to query the database table which was linked with that model.
Adding Additional Constraints
The method will return all model table results. You can also have the choice to add constraints. So that you can retrieve the results. But please review all other methods.
$users= App\User::where(‘active’, 1)
The collection class only provides a huge amount of useful methods that needs to work on your Eloquent methods.
In case if you need to access the thousands and thousands of eloquent results, use only the chunk command. It will retrieve the chuck of the each and every eloquent models.It will highly conserve the memory when you are accessing thousands of results.
The cursor allows you to iterate your records in the database .If you are make processing large amount of data the cursor method will reduce the memory usage.So it would be a great advantage.
Not Found Exceptions
There is also a situation, while you need to pass an exception , if a particular model is not found. It is an advantage on the controller.
You can use the sum, count and other aggregates methods which was provided by the query builders .Scalar values will be returned in this case.
$count = App\User::where(‘active’, 1)->count();
$max = App\User::where(‘active’, 1)->max(‘price’);
Deleting Models (Deleting a record in Laravel)
We can easily delete a model simply by calling the method on an instance of the model.
We have provided only some information on the above topic. Stay updated to know more.