LARAVEL 5.3 TUTORIAL PART-6: Basic Laravel Eloquent and Model

Introduction

The Eloquent ORM included with Laravel to provides a beautiful, simple Active Record implementation for working with your database. Each database table has a corresponding “Model” which is used to interact with that table.In this tutorial, we are going to look at how we can use Eloquent ORM to eloquently add and retrieve data from the database.

Creating Model

Models typically live in the app directory, but you are free to place them anywhere that can be auto-loaded according to your composer.json file. All Eloquent models extend from  Illuminate\Database\Eloquent\Model class.

In Laravel PHP Framework we can create Models for easy using the ‘Artisan’ CLI (Command Line Interface).


php artisan make:model User

Create Laravel Model

Create Laravel Model

 

New Model User.php will be place at app folder. You can see that source code as like below image:

Sample Laravel Model

Sample Laravel Model

 

Table name and primary key

By default, the plural form of the model name is used as the table name and the primary key field name is assumed to be id. This section shows you how you can explicitly define both the table and primary key field names Add the following lines to User model

protected $primaryKey = 'id';
protected $table = 'user';

 

Record timestamps

By default, Laravel assumes you have added the following fields to all of your database tables

  • created_at
  • updated_at

These fields are updated whenever you create a new record or update an existing record. If you do not want to use these timestamp fields in your database tables, you can set the following property to turn them off.

public $timestamps = false;

So finally your Model User Code look like below this:


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    //
    public $timestamps = false;
     protected $primaryKey = 'id';
     protected $table = 'user';
}

Our User Model Now Complete now for retrieve Data from User table, we want to create a controller

Creating Controller

Since reading our previous articles we know it’s so common to build applications around resources, you need to generate a resource controller — a controller to handle all requests related to a resource — using another simple Artisan command:


php artisan make:controller UserController

That will generate an app/Http/Controllers/UserController.php controller file with the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class UserController extends Controller
{
    //
}

Now, we just need to define routes to associate URLs with all these controller actions.

Creating Route

Again, since reading our previous article we know this is such a common scenario, you can define a single resource route, which creates routes for all of those resource controller actions.

In the routes file — routes/web.php — add the following to define a User resource route:

Route::get('/', [email protected]');

Updating UserController

Now, we need to create index method in our UserController  and connect User Model to retrieve data from database.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\User;
class UserController extends Controller
{
    //
    public function index(){

    	$users = User::all();

	foreach ($users as $user) {
    	echo $user->name,"<br>";
	}
    }
}

Now Visit the following URL to see the output

http://localhost:8000/

The output will appear as shown in the following image.

Sample Laravel Model Output

Sample Laravel Model Output

 

Thankyou

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I graduated in 2009 with a bachelor’s degree in Engineering from State University Of Bangladesh, I’m also a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, CIDA and MAX Group where I worked on ERP software and web development., but now i’m a co-founder and Chief Executive Officer and Managing Director of TechBeeo Software Consultancy Services Ltd. I’m also a Course Instructor of ZCE PHP 5.3 Certification and professional web development course at IBCS-PRIMAX Software (Bangladesh) Limited – a leading Training Institute in the country.
Print Friendly
One comment on “LARAVEL 5.3 TUTORIAL PART-6: Basic Laravel Eloquent and Model
  1. Regarding the last code example, in actual usage you would not output the users data from the controller. You would pass it to your view. So this:

    foreach ($users as $user) {
    echo $user->name,””;
    }

    Should be something like:

    return view(‘users.index’, compact(‘users’));

Leave a Reply

Your email address will not be published. Required fields are marked *

*