laravel 5.7 how to pass request of controller to model and save

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Passing Request Data from Controller to Model: Mastering Separation of Concerns in Laravel

As a senior developer, one of the most critical principles in building scalable and maintainable applications, especially within the Laravel ecosystem, is the strict separation of concerns. The controller's job should be to handle HTTP requests, delegate tasks, and manage responses, while the model's responsibility is solely to manage data persistence, business logic, and calculations.

The scenario you presented—passing request data from the controller to the model for saving—is a fundamental architectural pattern in Laravel. Let’s dive into how to achieve this cleanly, ensuring that your Model remains focused on the data layer, as is best practice when working with Eloquent.

The Controller-Model Relationship Explained

Your goal is to ensure the controller acts purely as an intermediary, sending necessary information to the model for processing and saving. You want the heavy lifting—the actual saving and any related calculations—to reside within the Model.

The confusion often arises because the $request object lives in the scope of the Controller, while the Model needs raw data. We need a mechanism to bridge this gap efficiently.

Method 1: The Eloquent Mass Assignment Approach (Quick & Simple)

For simple scenarios where you are just saving fields directly from the request, Laravel provides powerful shortcuts using Eloquent's mass assignment features. This is often the quickest route.

In your PostController.php:

use Illuminate\Http\Request;
use App\Models\Post; // Ensure you import your model

class PostController extends Controller
{
    public function store(Request $request)
    {
        // 1. Validate the request data first (Crucial step!)
        $validatedData = $request->validate([
            'title' => 'required|string',
            'description' => 'required|string',
        ]);

        // 2. Use create() to handle the saving logic directly in the Model
        // Eloquent handles mapping these fields directly to the database columns.
        $post = Post::create($validatedData);

        return redirect(route('post.index'));
    }
}

Why this works: This approach keeps the controller lean. It delegates the entire act of creating and saving the record to the Post model, adhering perfectly to the principle that the Model owns its data logic. When you work with Eloquent, as detailed in resources like those found on laravelcompany.com, these methods are designed for efficient data handling.

Method 2: Explicit Data Passing (For Complex Business Logic)

If your saving process involves more than just direct field mapping—for example, if you need to perform custom calculations before saving, sanitize data in a specific way, or interact with other related models within the Model—passing the request object directly is beneficial.

In this method, we pass the necessary data explicitly:

Controller (PostController.php):

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
    public function store(Request $request)
    {
        // 1. Validate the input
        $validatedData = $request->validate([
            'title' => 'required|string',
            'description' => 'required|string',
        ]);

        // 2. Pass the validated data directly to the Model method
        $post = Post::createFromRequest($request, $validatedData);

        return redirect(route('post.index'));
    }
}

Model (Post.php):

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * Custom method to handle the creation logic, performing calculations internally.
     *
     * @param array $data The data received from the controller.
     * @return \Illuminate\Database\Eloquent\Model
     */
    public static function createFromRequest(array $data)
    {
        // Example of internal business logic/calculation in the Model
        $title = $data['title'];
        $description = $data['description'];

        // Perform a calculation before saving (e.g., sanitizing text length or adding timestamps)
        if (strlen($title) > 100) {
            throw new \Exception("Title is too long.");
        }

        return static::create([
            'title' => $title,
            'description' => $description,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}

Conclusion: Where to Draw the Line

For most standard CRUD operations, Method 1 (Mass Assignment) is sufficient and idiomatic in Laravel. It keeps your controller clean by delegating persistence directly to Eloquent.

However, if you need complex validation, intricate data transformation, or custom business rules that depend heavily on the incoming request data, Method 2 (Explicit Passing) allows your Model to become a true business logic hub. This ensures that when you explore advanced frameworks, such as those provided by laravelcompany.com, you maintain a separation of concerns that scales effortlessly with your application's complexity. Always aim for the simplest solution that remains robust and maintainable!