How to create empty instance of Laravel Eloquent entity
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Create an Empty Instance of a Laravel Eloquent Entity for API Responses
As senior developers working with Laravel, we often encounter scenarios where the need arises to return data structures that mimic Eloquent models—complete with their properties and potential relationships—but without actually persisting anything to the database. This is extremely common when building RESTful APIs, especially when preparing data for a frontend interface (like a JavaScript application) before an actual `create` or `update` operation occurs.
The core challenge, as highlighted in your request, lies in how Laravel's Eloquent mechanism initializes a model instance and whether we can leverage this initialization to generate default values (like auto-incremented IDs or regional settings) for a "fresh" object.
This post will explore the pitfalls of creating empty instances and present robust, idiomatic solutions for generating these specialized JSON objects.
---
## The Pitfall: Why `new Model` Fails for API Responses
When you simply use `$contract = new Contract;`, PHP creates a standard, empty object. When you attempt to `json_encode()` this object, it results in an empty structure (`{}`) or an array, as there are no properties set by the Eloquent layer.
```php
// This results in an empty object when encoded: {} or []
$contract = new Contract;
return json_encode($contract);
```
This output does not satisfy the requirement of returning a structure populated with expected defaults (like a generated `ID` or default configuration values) that a frontend expects. We need a mechanism to trigger Eloquent's setup logic without involving the database write operation.
## Solution 1: Leveraging Model Factories for Controlled Instantiation
The most robust and idiomatic Laravel way to create an "empty" but fully structured model instance is by utilizing **Model Factories**. Factories are designed specifically for generating test data or seeding records, making them perfect tools for creating template objects.
By using a factory, you can define the structure and default values that Eloquent expects, allowing you to instantiate a model object with those predefined defaults immediately.
### Implementation Example
First, ensure you have a factory defined for your `Contract` model (e.g., in `database/factories/ContractFactory.php`).
In your controller method, instead of using `new Contract`, you use the factory helper:
```php
use App\Models\Contract;
class ContractsController extends Controller
{
public function create()
{
// Use the factory to create a new instance with defined defaults
$contract = Contract::factory()->make();
// Or, if you want to explicitly use the model's creation methods:
// $contract = Contract::new(); // Still results in an empty object unless manually populated
return response()->json($contract);
}
}
```
**Why this works:** While `make()` is typically used to generate data for database insertion, when used on a fresh model instance outside of a standard resource query, it leverages the underlying structure defined by Eloquent and its associated setup. For true "empty" structures where you only want defaults, we can combine this with manual population or utilize custom methods.
### Solution 2: Manual Population via Attributes (The Cleanest Approach)
For scenarios where you need truly empty data that is ready for the client to modify before a final save, manually defining the structure based on your model's `$fillable` attributes is often cleaner than relying solely on factory generation when no database interaction is intended.
You can create an empty array and populate it with default values matching your schema:
```php
use App\Models\Contract;
class ContractsController extends Controller
{
public function create()
{
// Define the structure based on the model's fillable attributes
$data = [
'ID' => null, // ID will be generated upon insertion, so we leave it null or unset
'CONTRACT_DATE' => null,
'PRICE_TYPE' => '2', // Example default value
'AMOUNT' => 0.0, // Example default value
];
// Create a new instance and hydrate it with the data
$contract = new Contract($data);
return response()->json($contract);
}
}
```
**Note on IDs:** If you absolutely need an ID generated by the database (like an auto-incremented primary key), this method will return `null` for the ID. In a frontend context, this is often desirable—the client knows it's a new record waiting to be inserted.
## Conclusion: Choosing the Right Tool
The choice of how to create an empty Eloquent instance depends entirely on your goal:
1. **For Data Generation/Seeding:** Use **Model Factories**. This ensures that the object structure adheres perfectly to what Eloquent expects, even if you are just mocking data.
2. **For Empty API Payloads (Ready for Insert):** Manually construct an array matching the `$fillable` attributes and pass it to the model constructor (`new Model($data)`). This gives you complete control over the initial state without involving ORM persistence logic, making the resulting JSON perfectly tailored for client-side interaction.
By understanding these distinctions, you can harness the power of Eloquent correctly, ensuring your API responses are not only functional but also developer-friendly and aligned with best practices in Laravel development. For more advanced insights into leveraging Eloquent's capabilities, always refer to the official documentation at [https://laravelcompany.com](https://laravelcompany.com).