Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Basic understanding of models in Laravel

Sunday, December 6th, 2015 by Servage

eloquentWhen working with Laravel you will soon realize that the Eloquent model system is a core functionality with lots of powerful features. It drives your entire object and database handling, and relieves you of repetitive tedious code writing. With Eloquent you can use structured data objects easily.

Creating new models

You can generate models with the “artisan” command in Laravel. Below is an example (with and without generation of a migration file):

php artisan make:model Product
php artisan make:model Product -m

It is convenient that you can generate the migration file right away if you are building a database model. The migration file is where you define the database changes required to facilitate a new or changed model. It is basically a description of changes to the database, so that you can up- or downgrade your database between different versions.

The model class

Normally you create models to extend the base Eloquent model. This is what the artisan command could generate for you.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    // Model methods here ... 
}

?>

You can place your methods inside the class, or even have an intermediary model for your app – or use traits – to add general logic to multiple of your models at once.

Database settings

The default table name is assumed to be the lower case plural version of the model class name (which is assumed to be camel case singular name). So if the “Product” class table would not be “products” according to the standard, but “other_products”, you can set it like below:

protected $table = 'other_products';

You can also change the primary key, database connection and timestamp fields, if you want automatic creation and/or update timestamps to be set.

Getting data

You can select models with smart methods.

$products = App\Product::where('active', 1)
               ->orderBy('name', 'desc')
               ->take(10)
               ->get();

The example above includes a condition, sorting and limitation. Without any of those you could also simply select all products like below:

$products = App\Product::get();

The return value of a selection will always be a collection if you choose multiple objects, or a single model instance if you choose a single object. Laravel is smart enough to know this, and returns the expected.

Adding and changing data

Whenever you edit a model attribute which correlates to a database table column, it can be saved into the database easily.

$product->name = 'New name';
$product->save();

Eloquent handles the work of finding out if this is a new record that needs to be inserted, or an existing record that needs to be updated.

Use the delete method to remove something from the database:

$product->delete();

For deletion you can choose to use soft delete, where records are marked as deleted instead of being actually deleted from the database. This is very useful for data which is required for some reason, even if deleted.

Eloquent is very powerful and this introduction shows some of the main uses and benefits. You can also use non-database models, or non-database attributes.

Basic understanding of models in Laravel, 4.2 out of 5 based on 5 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.