CodeClouds Color Logo CodeClouds White Logo

PHP, LARAVEL | 05 May 2017

PHP Laravel: Creating your first Model in Eloquent ORM

php framework

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!

Originally written May 05, 2017. Last updated September 4th, 2020

 Views

Written by Scott Hunley

Scott, as a DevOps Manager, works with senior management to ensure the performance objectives, daily production, and service levels are maintained.

  • facebook
  • twitter
  • linkedin
  • pinterest
  • whatsup
Related Articles
An Introduction to PHP, Its Uses, and Starting a Career With PHP

PHP | 27 October 2022

An Introduction to PHP, Its Uses, and Starting a Career With PHP

PHP handles back end logic for websites and web applications, and is the most common language that developers use to build the backend of the web as we know it. The difference between frontend languages like HTML and javascript is they are processed client side. PHP, as a backend language, is processed server side.

Why PHP is Great Choice for your Startup

WEB DEVELOPMENT, OPINION, PHP | 07 December 2020

Why PHP is Great Choice for your Startup

We love PHP at CodeClouds. It’s a powerful, highly extensible language that brings a lot to the table. If you’re building a new startup, it can be an excellent tool that provides you with a lot of powerful functionality; as far as server-side programming languages go, it strikes an excellent balance of power and accessibility that others envy. Today we’re going to be breaking down exactly why we think it’s a good choice for you, and why you should be using it in your startup.