Laravel Eloquent Make

Updated October 14, 2020・2 minute read Post By tnylea
Laravel Eloquent Make

I recently learned about the Laravel Eloquent Make command, which allows you to create an Eloquent object without adding it to the database.

To fully understand this, I'll start by creating a new Laravel app with a posts table.

Create a new Laravel App

We'll start things off with a new laravel application called blog.

laravel new blog

We'll then need to create a new migration, which we'll use to create our posts table.

Create a Posts Table

First, we'll create a new migration by running:

php artisan make:migration create_posts_table

We'll then add the following table data to our new migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('body');
            $table->string('slug')->unique();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Before running our migration, we need to connect to a database. We can add a new SQLite database to our database folder by running:

touch database/database.sqlite

Next, we need to change our DB connection to sqlite inside our .env file. We will also comment out the DB_DATABASE. This way, our app will use the default SQLite connection.

DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
#DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=

And now, we can run our migrations:

php artisan migrate

Now, we'll have our newly created posts table.

Using Eloquent Make command

To interact with our Posts table, we'll need to create a new Eloquent Model:

php artisan make:model Post

Now, we can easily make a new Post without adding it to the database with the following code:

$post = \App\Models\Post::make([
    'title' => 'Awesome Post Title',
    'slug' => 'awesome-post-title',
    'body' => 'This is the body of my awesome post'
]);

echo $post->title;

If we were to run that code, the title of Awesome Post Title, will be output.

You may get an error when you run the code above. Make sure to add an empty $gaurded array to your App\Model\Post controller, like so:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

    protected $guarded = [];
}

And, you'll be back in business. 👍

The reason this is cool is that we can interact with our post as if it were a real post, and if we decide, at any point, that we want to save this to the database, we could then run:

$post->save();

And that new post would now be saved to our database. How cool is that?

So, if you ever need to play around with the data of an Eloquent Model without saving it to the database, you can use the ::make() command to do just that. 😉

Still have a question?

Get help or discuss anything development/design related.

Start a discussion