a guide to using eloquent orm in laravel ♥ scotch

26
14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch data:text/html;charset=utf8,%3Cdiv%20class%3D%22tile%22%20style%3D%22boxsizing%3A%20borderbox%3B%20borderradius%3A%200px%200px… 1/26 A Guide to Using Eloquent ORM in Laravel Chris Sevilleja March 3, 2014 61 Comments crud,eloquent,laravel VIEW CODE The Eloquent ORM that comes with Laravel makes it incredibly easy to interact with a database. Today we’ll look at how we can use Eloquent to interact with our database and do: The basicCRUD functions Set up and useonetoone relationships Sample Application Share Tweet

Upload: juan-andres-jerez-balmaceda

Post on 25-Dec-2015

31 views

Category:

Documents


2 download

DESCRIPTION

laravel

TRANSCRIPT

Page 1: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 1/26

A Guide to Using Eloquent ORM inLaravel

Chris Sevilleja March 3, 2014 61 Comments crud,eloquent,laravel

VIEW CODE

The Eloquent ORM that comes with Laravel makes it incredibly easy to interactwith a database. Today we’ll look at how we can use Eloquent to interact withour database and do:

The basicCRUD functions

Set up and useone­to­one relationships

Sample Application

Share Tweet

Page 2: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 2/26

Set up and useone­to­many relationships

Set up and usemany­to­many relationships

How Does Eloquent Work?

The Eloquent ORM provides anActiveRecordimplementation to work with your database.

This means that each model you create in your MVC structure corresponds to a table in

your database.

A Bear model will correspond to a bears table in your database. Since we have

convention when creating our models and database tables, we can easily call data from

our database.

For example, to get all bears from a Bear model, all you would do is Bear::all() . Using

this function would query the database and generate the proper SQL command. Here are

some quick examples:

Super simple! There are many more applications of this and we’ll get into them farther

into this article.

Let’s create a sample application about bears . Let’s say there are many bears. There are

differenttypesof bears with differentweightsanddanger levels.

Sample Application

Page 3: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 3/26

We will also say there are fish . Eachfish belongs to a single bearsince bears don’t like

sharing. This will be ourone­to­one relationship.

Bears also love to climb trees . Each bear will have many trees they like to climb. This

will be ourone­to­many relationship.

There will also be picnics .Many bears can go to many picnicssince they like to

ransack as many delicious picnics as they can. This will be ourmany­to­many

relationship.

To get started using Eloquent, we’ll need to set up our database and application. Let’s

run through that real quick. We’ll need to:

Install Laravel: composer install ‐‐prefer‐dist

Change our database settings in app/config/database.php

Create migrations using artisan

Create Eloquent models

Seed our database

Those first two parts are easy enough so get those done. Then we’ll work on migrating

and seeding our database.

Laravel Setup

Page 4: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 4/26

With our migrations, we will be addingthree tablesto our database: bears , fish ,

and picnics . For more information on using migrations, read theLaravel docs.

Bears Migration

Let’s create our migration. Through the command line, type:

php artisan migrate:make create_bears_table ‐‐create=bears

Let’s go in and add our fields.

Creating Our Migrations

Migrations

// app/database/migrations/####_##_##_######_create_bears_table.php...

Schema::create('bears', function(Blueprint $table) $table‐>increments('id');

$table‐>string('name'); $table‐>string('type'); $table‐>integer('danger_level'); // this will be between 1‐10

$table‐>timestamps(););

...

Page 5: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 5/26

By default, these migrations will include an auto­incrementing id . It will also include

timestamps for the fields created_at and updated_at . updated_at will be automatically

updated whenever the record is updated.

Fish Migration

php artisan migrate:make create_fish_table ‐‐create=fish

Plural vs Singular Database Table NamesWith our bears, we can createthe standard plural table name (ie bears, picnics). With fish, it’s different.Do we use fish or fishes? The good news is we can use whichever we wantand thenoverride the defaultswhen defining our Eloquent model .

Tree Migration

// app/database/migrations/####_##_##_######_create_fish_table.php...

Schema::create('fish', function(Blueprint $table) $table‐>increments('id');

$table‐>integer('weight'); // we'll use this to demonstrate searching by weight $table‐>integer('bear_id'); // this will contain our foreign key to the bears table

$table‐>timestamps(););

...

Page 6: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 6/26

php artisan migrate:make create_trees_table ‐‐create=trees

Picnic Migration

Since we will be creating a one to many relationship, we will need two tables. One for

thepicnicsand another to link a bear to a picnic.

php artisan migrate:make create_picnics_table ‐‐create=picnics

// app/database/migrations/####_##_##_######_create_trees_table.php...

Schema::create('trees', function(Blueprint $table) $table‐>increments('id');

$table‐>string('type'); $table‐>integer('age'); // how old is the tree $table‐>integer('bear_id'); // which bear climbs this tree

$table‐>timestamps(););

...

// app/database/migrations/####_##_##_######_create_picnics_table.php...

Schema::create('picnics', function(Blueprint $table) $table‐>increments('id');

$table‐>string('name'); $table‐>integer('taste_level'); // how tasty is this picnic?

Page 7: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 7/26

Now we will need a table to link our bears to a picnic. We’ll create a pivot table here. This

is how we can define our many­to­many relationship.

php artisan migrate:make create_bears_picnics_table ‐‐create=bears_picnics

Now we have a way to link our multiple bears to multiple picnics. This is how we create

our many to many relationship.

Migrating Our Database

$table‐>timestamps(););

...

// app/database/migrations/####_##_##_######_create_bears_picnics_table.php...

Schema::create('bears_picnics', function(Blueprint $table) $table‐>increments('id');

$table‐>integer('bear_id'); // the id of the bear $table‐>integer('picnic_id'); // the id of the picnic that this bear is at

$table‐>timestamps(););

...

Page 8: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 8/26

With our migration files made, migrate your database using artisan:

php artisan migrate

Now that we have migrated our database, we will need to seed our database. The

process of seeding however is inserting records into our database and this will require

Eloquent! We will need to create our models before we can seed the database.

Let’s make our Eloquent models. This is also where we will define our relationships.

Bear Model

Let’s look at our Bear model first.

// app/models/Bear.php<?php

class Bear extends Eloquent // MASS ASSIGNMENT ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ // define which attributes are mass assignable (for security)

Eloquent Models

Page 9: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 9/26

// we only want these 3 attributes able to be filled protected $fillable = array('name', 'type', 'danger_level');

// DEFINE RELATIONSHIPS ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ // each bear HAS one fish to eat public function fish() return $this‐>hasOne('Fish'); // this matches the Eloquent model

// each bear climbs many trees public function trees() return $this‐>hasMany('Tree');

// each bear BELONGS to many picnic // define our pivot table also public function picnics() return $this‐>belongsToMany('Picnic', 'bears_picnics', 'bear_id', 'picnic_id');

Mass AssignmentWe have to set ourmass assignable attributesso that we make sure

that only the attributes we want are allowed to be changed.

Defining RelationshipsWhen defining relationships, the name of the function can be

whatever you want it to be named. It makes sense here since we will be finding

the fish that belongs to the bear. On the line return $this‐>hasOne('Fish') however,

you will need to match the name of the Eloquent model that corresponds to that item.

There are different ways we can define relationships. There

are hasOne , hasMany , belongsTo , belongsToMany , and more. Read theEloquent

Relationship docsto see all the things you can do.

Eloquent Model and Database Table Naming ConventionsBy default, when you define

Page 10: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 10/26

an Eloquent model, you name it for thesingularterm. In this case Bear . Eloquent will then

look to the database for thelowercase and pluralversion of that word. In this case, this

model will be linked to our bears table we created in our migration.

Fish Model

Here is our Fish model.

// app/models/Fish.php<?php

class Fish extends Eloquent // MASS ASSIGNMENT ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ // define which attributes are mass assignable (for security) // we only want these 3 attributes able to be filled protected $fillable = array('weight', 'bear_id');

// LINK THIS MODEL TO OUR DATABASE TABLE ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ // since the plural of fish isnt what we named our database table we have to define it protected $table = 'fish';

// DEFINE RELATIONSHIPS ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ public function bear() return $this‐>belongsTo('Bear');

Like we talked about earlier, since we named our table fish , then it doesn’t follow

convention. We will explicitly call out the database name using protected $table .

Similar to how we defined our relationship in the Bear model, we will define the inverse of

Page 11: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 11/26

that relationship. A Fish belongs to a Bear .

Tree Model

// app/models/Tree.php<?php

class Tree extends Eloquent // MASS ASSIGNMENT ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ // define which attributes are mass assignable (for security) // we only want these 3 attributes able to be filled protected $fillable = array('type', 'age', 'bear_id');

// DEFINE RELATIONSHIPS ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ public function bear() return $this‐>belongsTo('Bear');

Picnic Model

You know the drill now. Let’s make the Picnic model.

// app/models/Picnic.php<?php

class Picnic extends Eloquent // MASS ASSIGNMENT ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ // define which attributes are mass assignable (for security) // we only want these 3 attributes able to be filled protected $fillable = array('name', 'taste_level');

Page 12: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 12/26

// DEFINE RELATIONSHIPS ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ // define a many to many relationship // also call the linking table public function bears() return $this‐>belongsToMany('Bear', 'bears_picnics', 'picnic_id', 'bear_id');

Just like our other models, we have defined mass assignable attributes and relationships.

When defining many­to­many relationships, you use belongsToMany() and not

hasMany. hasMany is used for one­to­many relationships.

Now that we have our migrations and models done, we can seed our database. We will

use Eloquent forinsertinginto our database for our seeds.

For more information on Eloquent concepts likecreating Eloquent models,performing

CRUD, ordefining relationships, definitely read theLaravel Eloquent docs.

Seeding Our Database

For those of you who don’t know,seedingallows us to fill our database with dummy

information to play with. This is great when developing our applications and need data to

populate our application.

We will createdatabase seedersinside of the app/database/seeds/DatabaseSeeder.php .

Usually you would want to create separate seed files, but we’re going to dump everything

into this file to make this simple. (We want to get to querying things with Eloquent

already!)

Page 13: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 13/26

// app/database/seeds/DatabaseSeeder.php<?php

class DatabaseSeeder extends Seeder

/** * Run the database seeds. * * @return void */ public function run() Eloquent::unguard();

// call our class and run our seeds $this‐>call('BearAppSeeder'); $this‐>command‐>info('Bear app seeds finished.'); // show information in the command line after everything is run

// our own seeder class// usually this would be its own fileclass BearAppSeeder extends Seeder public function run()

// clear our database ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ DB::table('bears')‐>delete(); DB::table('fish')‐>delete(); DB::table('picnics')‐>delete(); DB::table('trees')‐>delete(); DB::table('bears_picnics')‐>delete();

// seed our bears table ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ // we'll create three different bears

// bear 1 is named Lawly. She is extremely dangerous. Especially when hungry. $bearLawly = Bear::create(array( 'name' => 'Lawly', 'type' => 'Grizzly', 'danger_level' => 8

Page 14: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 14/26

));

// bear 2 is named Cerms. He has a loud growl but is pretty much harmless. $bearCerms = Bear::create(array( 'name' => 'Cerms', 'type' => 'Black', 'danger_level' => 4 ));

// bear 3 is named Adobot. He is a polar bear. He drinks vodka. $bearAdobot = Bear::create(array( 'name' => 'Adobot', 'type' => 'Polar', 'danger_level' => 3 ));

$this‐>command‐>info('The bears are alive!');

// seed our fish table ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ // our fish wont have names... because theyre going to be eaten

// we will use the variables we used to create the bears to get their id

Fish::create(array( 'weight' => 5, 'bear_id' => $bearLawly‐>id )); Fish::create(array( 'weight' => 12, 'bear_id' => $bearCerms‐>id )); Fish::create(array( 'weight' => 4, 'bear_id' => $bearAdobot‐>id )); $this‐>command‐>info('They are eating fish!');

// seed our trees table ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Tree::create(array( 'type' => 'Redwood', 'age' => 500, 'bear_id' => $bearLawly‐>id )); Tree::create(array(

Page 15: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 15/26

'type' => 'Oak', 'age' => 400, 'bear_id' => $bearLawly‐>id ));

$this‐>command‐>info('Climb bears! Be free!');

// seed our picnics table ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

// we will create one picnic and apply all bears to this one picnic $picnicYellowstone = Picnic::create(array( 'name' => 'Yellowstone', 'taste_level' => 6 )); $picnicGrandCanyon = Picnic::create(array( 'name' => 'Grand Canyon', 'taste_level' => 5 )); // link our bears to picnics ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ // for our purposes we'll just add all bears to both picnics for our many to many relationship $bearLawly‐>picnics()‐>attach($picnicYellowstone‐>id); $bearLawly‐>picnics()‐>attach($picnicGrandCanyon‐>id);

$bearCerms‐>picnics()‐>attach($picnicYellowstone‐>id); $bearCerms‐>picnics()‐>attach($picnicGrandCanyon‐>id);

$bearAdobot‐>picnics()‐>attach($picnicYellowstone‐>id); $bearAdobot‐>picnics()‐>attach($picnicGrandCanyon‐>id);

$this‐>command‐>info('They are terrorizing picnics!');

Seeding a Database with Relationships

In our seeder file, we are creating bears, fish, picnics, and linking many bears to one

Page 16: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 16/26

picnic.

Grabbing the ID of a Newly Created RecordWe need to grab the id of the inserted

bears and picnic so we will save the record into a variable on creation. After doing this,

we are able to pull the id using $bearLawly‐>id .

Why do we do this? Why do we pull the id of a newly created record? There are a few

reasons for this. One, so we can create our relationships correctly. Second, after seeding

your database multiple times, the id of your records will always be incrementing since

that’s how we set up our database. As you can see in the picture below, the id of our

bears are 10 , 11 , and 12 . Dynamically creating our relationships instead of hardcoding in

the id lets us not worry about messing with our seed files after they have been created.

With our seeder file ready to go, let’s go into the command line and execute our seeds.

php artisan db:seed

We can also look into our database and see the new records.

Page 17: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 17/26

Just like that, we now have records in our database. We can finally get to the fun part and

show off the true power of Eloquent! Finally!

Next, we will go through all the types of queries you can create with Eloquent and we will

see how easy it is to query databases with one­to­one and many­to­many relationships.

With migrations and seeding finally done, we’ll go through some basic real world

scenarios where we would need to access the information we’ve just put into our new

database.

With Eloquent, it is that easy to create records for our database. Just call your model and

the function you need. It is also incredibly easy to read, update, or delete records out of

our database. Let’s look at CRUD functionality with Eloquent.

Creating Records

Using EloquentQuerying our Database

CRUD With Eloquent

Page 18: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 18/26

You’ve already seen how to create records since we used the ::create method in our

seeders.

// create a bear Bear::create(array( 'name' => 'Super Cool', 'type' => 'Black', 'danger_level' => 1 ));

// alternatively you can create an object, assign values, then save $bear = new Bear;

$bear‐>name = 'Super Cool'; $bear‐>type = 'Black'; $bear‐>danger_level = 1;

// save the bear to the database $bear‐>save();

In addition to the create method, you can also create a new object and assign different

attributes to it. Once that is over you can call the save() function.

Another method for creation is using firstOrCreate() or firstOrNew() . These will let us

try to find a bear with certain attributes, if that bear is not found, then we will either create

it into the database or instantiate a new instance.

// find the bear or create it into the database Bear::firstOrCreate(array('name' => 'Lawly'));

// find the bear or instantiate a new instance into the object we want $bear = Bear::firstOrNew(array('name' => 'Cerms'));

Page 19: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 19/26

Getting and Finding Records

We can build all of our queries simply. Select statements, get alls, and finding records are

all doable and easy.

Here are a few examples of use cases.

// get all the bears $bears = Bear::all();

// find a specific bear by id $bear = Bear::find(1);

// find a bear by a specific attribute $bearLawly = Bear::where('name', '=', 'Lawly')‐>first();

// find a bear with danger level greater than 5 $dangerousBears = Bear::where('danger_level', '>', 5)‐>get();

First vs GetWhen querying the database and creating where statements, you will have to

use get() or first() . First will return only one record and get will return an array of

records that you will have to loop over.

Updating Records

To update a record, just find the record you’d like to update, change the attributes, and

save. Super simple!

Page 20: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 20/26

// let's change the danger level of Lawly to level 10

// find the bear $lawly = Bear::where('name', '=', 'Lawly')‐>first();

// change the attribute $lawly‐>danger_level = 10;

// save to our database $lawly‐>save();

Deleting Records

Deleting records might be easier than updating records. There are two methods: pull the

record you want and delete it or just use the destroy method.

// find and delete a record $bear = Bear::find(1); $bear‐>delete();

// delete a record Bear::destroy(1);

// delete multiple records Bear::destroy(1, 2, 3);

// find and delete all bears with a danger level over 5 Bear::where('danger_level', '>', 5)‐>delete();

Page 21: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 21/26

Now this is where Eloquent gets fun. With most applications, you will have relationships

amongst the parts of your database. We have already defined these:bears will have one

fishandpicnics will have many bears.

One to One Relationship

Let’s see how we can use Eloquent to query these relationships and get our bears

something to eat! Since we defined the relationships in our models, querying will be

incredibly easy.

// find a bear named Adobot $adobot = Bear::where('name', '=', 'Adobot')‐>first();

// get the fish that Adobot has $fish = $adobot‐>fish;

// get the weight of the fish Adobot is going to eat $fish‐>weight;

// alternatively you could go straight to the weight attribute $adobot‐>fish‐>weight;

One to Many Relationships

For this example, let’s look at all the trees that our Lawly bear climbs.

Querying Relationships

Page 22: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 22/26

// find the trees lawly climbs $lawly = Bear::where('name', '=', 'Lawly')‐>first();

foreach ($lawly‐>trees as $tree) echo $tree‐>type . ' ' . $tree‐>age;

Many to Many Relationship

For this example, we will get the picnics that our Cerms bear goes to. We can also get all

the bears that go to the Yellowstone picnic.

// get the picnics that Cerms goes to ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ $cerms = Bear::where('name', '=', 'Cerms')‐>first();

// get the picnics and their names and taste levels foreach ($cerms‐>picnics as $picnic) echo $picnic‐>name . ' ' . $picnic‐>taste_level;

// get the bears that go to the Grand Canyon picnic ‐‐‐‐‐‐‐‐‐‐‐‐‐ $grandCanyon = Picnic::where('name', '=', 'Grand Canyon')‐>first();

// show the bears foreach ($grandCanyon‐>bears as $bear) echo $bear‐>name . ' ' . $bear‐>type . ' ' . $bear‐>danger_level;

As you can see, by setting up ourEloquent Models, we can easily query our database.

Page 23: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 23/26

Let’s show all this off in an actual view file so we can see how we pass this data to our

Laravel view. What good is all this work if we don’t show our users right?

We will need two things: arouteand aview file. Let’s run through these quickly to show off

all the great Eloquent things we just learned.

Our Route

// app/routes.php

... // create our route, return a view file (app/views/eloquent.blade.php) // we will also send the records we want to the view

Route::get('eloquent', function()

return View::make('eloquent')

// all the bears (will also return the fish, trees, and picnics that belong to them) ‐>with('bears', Bear::all()‐>with('trees', 'picnics'));

);

Our View File

We will useLaravel Blade Templatingto loop through our data and show it off in our view

now. Let’s create our view file at app/views/eloquent.blade.php .

Demonstration

Page 24: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 24/26

<!‐‐ app/views/eloquent.blade.php ‐‐>

<!doctype html><html lang="en"><head> <meta charset="UTF‐8"> <title>Eloquent Bears</title>

<!‐‐ CSS ‐‐> <!‐‐ BOOTSTRAP ‐‐> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> <style> body padding‐top:50px; /* add some padding to the top of our site */ </style></head><body class="container"><div class="col‐sm‐8 col‐sm‐offset‐2">

<!‐‐ BEARS ‐‐> <!‐‐ loop over the bears and show off some things ‐‐> @foreach ($bears as $bear)

<!‐‐ GET OUR BASIC BEAR INFORMATION ‐‐> <h2> $bear‐>name <small> $bear‐>type : Level $bear‐>danger_level </small></h2>

<!‐‐ SHOW OFF THE TREES ‐‐> <h4>Trees</h4> @foreach ($bear‐>trees as $tree) <p> $tree‐>type </p> @endforeach

<!‐‐ SHOW OFF THE PICNICS ‐‐> <h4>Picnics</h4> @foreach ($bear‐>picnics as $picnic) <p> $picnic‐>name : Taste Level $picnic‐>taste_level </p> @endforeach

@endforeach

</div></body>

Page 25: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 25/26

</html>

Now when you go view your app in your browser at: http://example.com/eloquent , you

will see all the data being pulled from your database.

Well that was a lot of information! Thanks for reading along and hopefully all this

information has given you a good primer on how to use Eloquent for a real world

application. We’ve gone through:

Migrations

Eloquent models

Seeding

Defining relationships

Querying our database

Querying relationships

While we have covered a good many topics, there is still so much more you can do when

dealing with databases in your application. For more information, I always encourage

reading through the all theEloquent docs. You can dive into things like pivot tables,

polymorphic relationships, advanced querying, and so much more.

Also, for more reading on actual applications that use the database, check out our tutorial

onLaravel CRUD with Resource Controllers.

Conclusion

Page 26: A Guide to Using Eloquent ORM in Laravel ♥ Scotch

14/2/2015 A Guide to Using Eloquent ORM in Laravel ♥ Scotch

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22tile%22%20style%3D%22box­sizing%3A%20border­box%3B%20border­radius%3A%200px%200px… 26/26