Laravel Eloquent

Laravel Eloquent

 

The Eloquent ORM included with Laravel gives a wonderful, basic ActiveRecord usage for working along with your database. With eloquent, each table in the database corporate a comparing “Model” that does utilize to connect with that table. 

The model permits you to insert, update and delete the records from the table to recover or fetch records from the table. They give the advantage to perform common database operations without encoding lengthy SQL queries. Eloquent provides no need to write SQL queries and all work can be done by defining tables and the relationship between those tables.

#Generating Model Class:

To create an Eloquent model, use the make: model Artisan command:

PHP artisan make: model Users

Models generated by the make: model command are going to be placed within the app/module directory. An example of Users model is given below:

<?php 

namespace App;

use IlluminateDatabaseEloquentModel;

class Users extends Model {

#code

}

If you’d wish to generate a database migration once you generate the model, you may use the –migration or -m command. When a model is generated, use the artisan command to generate a database migration.

php artisan make: model –migration

Eloquent Model Explanation:

You may generate varied alternative forms of classes once generating a model, like factories, seeders, and controllers. Additionally, these choices are also combined to make multiple classes at once

# to generate a model and a UsersFactory class….

php artisan make: model Users –factory

or

php artisan make:model Users -f

# to generate a model and a UsersSeeders class….

php artisan make: model Users  –seed

or

php artisan make: model Users -s

#  to generate a model and a UsersController class..

php artisan make: model Users –controller

or

PHP artisan make: model Users -c

  1. Table name :

By convention, the plural name of the class is going to be used as the table name by default. So, eloquent can assume the Users model stores records within the user’s table. And to define a model’s table name manually use table property on the model.

<?php namespace App;

use IlluminateDatabaseEloquentModel;

class Users extends Model {

protected $table =  ‘all_users’;

}

  1. Primary Keys :

Eloquent also will assume that each model’s corresponding table has a primary key column “id”.  If needed, you can define a different column as the primary key using the $primaryKey property on your model.

<?php namespace App;

use IlluminateDatabaseEloquentModel;

class Users extends Model {

protected $primaryKey =  ‘userid’;

}

  1. Timestamps :

Eloquent automatically sets values of created_at  and updated_at columns whenever models are created and updated. If you do not need these columns to manage automatically, use $timestamps  property on your model with a false value. 

<?php namespace App;

use IlluminateDatabaseEloquentModel;

class Users extends Model {

protected $timestamps =  false;

}

CRUD with Eloquent

CRUD stands for Create,  Read, Update and  Delete. It makes work easier for developers to work with multiple databases.

# to create records:

:: create method is used to insert a new record in table.

user_record::create(array(      ‘name’ => ‘Jane’,

      ‘gender’ => ‘female’,

      ‘rank’ =>5));

Methods like save(), firstOrCreate() or firstOrNew() are also the options to create a new record.

# using save() method$user = new Users;

$user->name => ‘John;

$user->gender => ‘male;

$user->rank  = 1;

$user->save();

#  to get user by name or create if doesn’t exist…

$user_record:: firstOrCreate ([

      ‘name’ => ‘Jane’,

]);

#  to get used by name or create if doesn’t exist with more attributes…

$user_record:: firstOrNew (

     [ ‘name’ => ‘John’],

     [ ‘gender => ‘male’, ‘rank’ =>2],); 

# to retrieve records:

Eloquent makes it easy and manageable t retrieve records from the database. To create :where statement use following methods:

all(): it returns all records from table.

$usersList = Users::all();

get(): it returns an array of records.

$female-users = Users::where(‘gender’, ’=’ , ’female’)->get();

first(): it returns only one record from table.

$female-users = Users::where(‘gender’, ’=’ , ’female’)->first();

find(): this method find record based on specific attribute.

$getusers = Users:: find(1);

# to update records:

Eloquent makes it easier to update records. Updating a record includes two processes. The first is retrieving a record and the second is t set any attribute you want to update. And by calling the save() method you can update record in the database  

$updateUser = Users::where(‘name’, ‘=’, ‘John’)->first();$updateUser->rank  = 1;

$updateUser->save();

# to delete records:

Eloquent provides an easy way to delete records. To achieve this there are two methods:

delete(): In this method, we retrieve the model before calling the delete method.

$user = Users::find(1);$user->delete();

destroy() : This method will accept multiple primary keys and delete records without retrieving them.

Users->destroy(1,2,3);

 

 

Leave a Reply