Web Development 3 min read

PHP Laravel: Creating your first Model in Eloquent ORM

Written on 05 May 2017
 
php framework
 
Overview

Laravel’s Eloquent ORM is an ActiveRecord implementation for interacting with your database. Each table in your database corresponds with a Model that your can use to interact with it. Using Models, you can execute queries or add new records to a table. Today, we’ll set up and test a basic Model.

Creating the Model

To start, you’ll need to be sure your database is configured correctly in config/database.php
Models can be created anywhere your composer.json specifies can be auto loaded. The normal place to put them, however, is the app directory. A Model can be created using the artisan command-
php artisan make:model TestModel
 
The file for this model will be in app/models/TestModel.php
The contents will look like this
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class TestModel extends Model {
}
?>
 

Choose the Correct Database Connection

Eloquent will use the default connection specified in your config/database.php file. Let’s specify
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class TestModel extends Model { protected $connection = 'your-connections-name'; }
 

Specifying a Table

As currently implemented, Eloquent will add an ‘s’ to your model name and look for the default table. In this case, it will be looking for a testmodels table. We can change that by specifying a table.
<?php
  namespace App;
  use Illuminate\Database\Eloquent\Model;

class TestModel extends Model { protected $connection = 'your-connections-name'; protected $table = 'test_table'; }
You will need to make sure you have a table in this database that matches all of the parameters you’ve specified here.

Setting the Primary Key

Eloquent will also make an assumption about the table’s primary key as “id” Let’s change that.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class TestModel extends Model { protected $connection = 'your-connections-name'; protected $table = 'test_table'; protected $keyType = 'pkey_name'; }

Testing your Model

The following is a basic test query
<?php
$TestModel = App\TestModel::all();
foreach ($TestModel as $TestData) {
    echo $TestData->field1;
}
This will echo the specified field (field1) for every entry in the table. Let’s sort our query and limit our results to the first 20
<?php
$TestModel = App\TestModel::orderBy('field2', 'asc')
 	->take(20)
        ->get();
foreach ($TestModel as $TestData) {
    echo $TestData->field1;
}
This will sort the table by field2, and display the first 20 results for field1. For more information on building queries with Eloquent, refer to the official query documentation.
Now that you’ve created your first Model and have used it queries on the linked database table, you’re ready to get started with Eloquent ORM in Laravel! The next steps are learning more complex queries, insertions, and integrating this all into your project.
laravel development company
If you need an experienced Laravel development company, trust the experts at CodeClouds! We provide 18 hours of support 5 days a week, and can do other related tasks including the webpage design of your Laravel based project. Contact us today!

Share this article

  • twittertwitter
242 reads
Contents

Similar Reads