Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fsdolphin's avatar

What I Understand About Migrations and Eloquent in Laravel

Hi,

As you can imagine I'm new in the Laravel world and PHP in general. I have been watching Jeff's tutorials on the Laravel fundamentals and I want to make sure I'm going in the right direction especial with Migrations and Eloquent.

Migrations: Every documentation about migrations describe it as follow...

Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current bla bla bla...

In a short statement and with my own words this is how I understand it. By using Migrations you will be able to use the Eloquent commands, which at the end will make your life much easier, right? Again this is besides of what every document already states about the version control benefits etc. Is my assumption close?

Eloquent: The way I understand Eloquent is as follow. If you setup you database using Migrations, Eloquent will help you with your queries in an elegant way, right? In other words in a simpler way to describe it, Eloquent is the equivalent to SQL commands but in a much simpler way.

How much of the above is true?

Thank you all for your help.

0 likes
6 replies
pmall's avatar
pmall
Best Answer
Level 56

Migrations are just a way to create the database structure and to version its modifications. It is totally unrelated from eloquent. You can use eloquent without migrations and migrations without eloquent.

Versioning the structure means, if your app needs another column on some table for its next version, by adding it with a migration you can keep both the code and the database structure in sync. If later you have to get back to an older version of your code with a different database structure, you rollback the migration and tada, everything is in sync.

1 like
JarekTkaczyk's avatar

@fsdolphin

  1. No
  2. No

Eloquent is an ORM that uses Query\Builder behind the scenes. The latter is the one responsible for making querying DB easier by abstracting the queries and using unified API for all the drivers (MySQL, Postgres, SqlServer etc) - in short. That said, Eloquent makes your life easy if you want to map your db data to the object-oriented code.

Migrations, on the other hand, have nothing to do with Eloquent or Query\Builder or whatsoever. All they do is keeping track of the changes to your DB schema, so you can run all of them and get your db to the point, where you left it. The same as your code repo (say GIT).

Imagine you have to checkout your application at some point, but the database has been manually changes since then - everything is fked up, because your code refers to different version of your schema - that's why you want to use migrations.

1 like
fsdolphin's avatar

Thank you both for the clarification about Migrations with the fact that it has nothing to do with Eloquent.

@JarekTkaczyk Now, as far as Eloquent, I still see it as an easy way to do your database queries, regardless of what database you are using. Sorry if I still don't get it.

Thanks

JarekTkaczyk's avatar

@fsdolphin I suggest you subscribe for this, and soon you will know ins and outs of Eloquent https://leanpub.com/eloquence-emerging

Now, I will show you what Eloquent does for you and what it does not (by itself) - it is covered in the preface of the book:

// Query\Builder
$rows = DB::table('some_table')->where(...)->orderBy(...)->select(...)->join(...)->get();

// Eloquent
$models = SomeModel::where(...)->orderBy(...)->select(...)->join(...)->get();

basically all the methods above belong to the Query\Builder class, no Eloquent feature was used so far, but you already have handful of methods for abstracting your queries. That said, you don't need to leverage Eloquent, if you just want to write your queries easily.

Now, here's what Eloquent can do for you additionally (just a little bit of its powers):

$user = App\User::find($someId); // row is fetched and stored as PHP object with Eloquent features
$user->email = 'new@email.com'; // assign some values to the attributes that reflect table columns
$user->first_name = 'Newname';
$user->save(); // save the updated values in the table

$user->posts; // fetch and return a Collection of Post models, assuming we've setup a relation
$user->posts()->save( // save a new post and associate it to the user
    new Post(['title' => 'Some long title of my new post', 'body' => ' .... ']) 
);

// assuming we have first_name and last_name columns in the table
// but we want to print full name easily:
$user->full_name; // using Eloquents accessor:

public function getFullNameAttribute()
{
    return $this->first_name . ' ' . $this->last_name;
}

// let's use query scopes:
$users = App\User::latest()->active()->adults()->take(10)->get();

// latest is generic method on the Builder class, but active and adults are not.
// we define them on the model in order to make the code easier to write and read.
// example:
public function active($query)
{
    $query->where('active', true);
}

public function adults($query)
{
    $today = date('Y-m-d');

    // assuming MySQL and 18yrs is the age we're looking for
    $query->whereRaw('timestampdiff (year, born_on, ?) >= 18', [$today]); 
}

@pmall bon travail, gaillard! ;)

1 like
fsdolphin's avatar

That sounds like a good book to own, I will definitely subscribe. Thanks a lot for the examples.

Please or to participate in this conversation.