Tutorial by Examples

Eloquent is the ORM built into the Laravel framework. It allows you to interact with your database tables in an object-oriented manner, by use of the ActiveRecord pattern. A single model class usually maps to a single database table, and also relationships of different types (one-to-one, one-to-man...
In addition to reading data with Eloquent, you can also use it to insert or update data with the save() method. If you have created a new model instance then the record will be inserted; otherwise, if you have retrieved a model from the database and set new values, it will be updated. In this exa...
You can delete data after writing it to the database. You can either delete a model instance if you have retrieved one, or specify conditions for which records to delete. To delete a model instance, retrieve it and call the delete() method: $user = User::find(1); $user->delete(); Alternat...
By default, Eloquent models expect for the primary key to be named 'id'. If that is not your case, you can change the name of your primary key by specifying the $primaryKey property. class Citizen extends Model { protected $primaryKey = 'socialSecurityNo'; // ... } Now, any Eloqu...
If you want to automatically throw an exception when searching for a record that isn't found on a modal, you can use either Vehicle::findOrFail(1); or Vehicle::where('make', 'ford')->firstOrFail(); If a record with the primary key of 1 is not found, a ModelNotFoundException is thrown. W...
You may find yourself needing to clone a row, maybe change a few attributes but you need an efficient way to keep things DRY. Laravel provides a sort of 'hidden' method to allow you to do this functionality. Though it is completely undocumented, you need to search through the API to find it. Using ...

Page 1 of 1