How to call model function into my controller in laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Call Model Functions into Your Controller in Laravel: The Right Way

As developers working within the Laravel ecosystem, understanding how to properly interact between Controllers and Models is fundamental. When you have a logic-heavy Model and need to execute its methods from your Controller, it’s easy to get stuck trying to instantiate classes manually. This guide will walk you through the correct architectural patterns for calling Model functions in Laravel, moving beyond simple instantiation to embrace Dependency Injection and Eloquent principles.

The Pitfall of Manual Instantiation

Let's first analyze the approach you attempted: creating a new instance of your UserModel inside the Controller's constructor. While technically possible in PHP, this pattern often violates Laravel’s design philosophy, which favors a more decoupled and service-oriented structure.

Your attempt involved:

// In UserController.php
public function __construct(){
    $users = new UserModel(); // Manual instantiation
}

And then trying to access methods via $this->users->getUserInformation($user_id);. This approach creates tight coupling between the Controller and the specific Model, making testing harder and reducing flexibility. In a larger application, this is generally an anti-pattern we want to avoid when building robust systems, much like adhering to Laravel best practices for structuring your application.

The Recommended Laravel Approach: Direct Access and Dependency Injection

In most standard Laravel applications, you do not need to manually instantiate Models within the Controller. Because Models extend Illuminate\Database\Eloquent\Model, they are designed to be accessed directly using the use statement at the top of your controller file. This allows Laravel’s service container to manage the Model instances efficiently.

1. Using Eloquent Directly (The Simplest Way)

If your model method is a simple query or a calculation based on the database, you can call it directly on the injected Model class.

First, ensure your UserModel is set up correctly. For demonstration purposes, let's adjust the model to reflect standard Eloquent usage:

app/Models/UserModel.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserModel extends Model
{
    // Note: We typically don't put custom logic methods directly on the model 
    // unless they are complex Eloquent scopes or accessors.
    public function getUserInformation($id)
    {
        // In a real scenario, this would involve Eloquent queries:
        return "User id is : " . $id . " (Data retrieved via Eloquent)";
    }   
}

app/Http/Controllers/UserController.php (Corrected)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\UserModel; // Import the Model
use Illuminate\Support\Facades\Auth;

class UserController extends Controller {

    // No need for constructor instantiation here if we are just calling methods.

    public function index(Request $request)
    {
        // Get data from request, ensuring proper input handling is vital in production code.
        $user_id = $request->input('id', 1); // Use Request object instead of raw $_GET for safety
        
        // Direct call on the imported Model class
        $userInfo = UserModel::getUserInformation($user_id); 
        
        return view('profile.users', ['user_details' => $userInfo]);
    }
}

2. Leveraging Dependency Injection (The Best Practice)

For more complex interactions, especially when dealing with services or repositories that contain business logic, the ideal pattern is Dependency Injection (DI). This practice promotes loose coupling, making your code cleaner, easier to test, and aligns perfectly with modern application design principles promoted by Laravel.

Instead of instantiating the Model in the Controller, you should inject the necessary service or model into the Controller's constructor. The framework handles creating and providing that instance for you.

app/Http/Controllers/UserController.php (DI Example)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\UserModel; // We still reference the model
use Illuminate\Support\ServiceProvider; // Often used for service binding setup

class UserController extends Controller {

    protected $userModel;

    // Inject the UserModel via the constructor
    public function __construct(UserModel $userModel)
    {
        $this->userModel = $userModel;
    }

    public function index(Request $request)
    {
        $user_id = $request->input('id', 1);
        
        // Call the method on the injected instance
        $userInfo = $this->userModel->getUserInformation($user_id); 
        
        return view('profile.users', ['user_details' => $userInfo]);
    }
}

To make this Dependency Injection work seamlessly, you would typically register this binding in your Service Provider (e.g., AppServiceProvider), which is a core concept when building scalable applications on Laravel. Learning about service container bindings is key to mastering the power of laravelcompany.com.

Conclusion

To summarize, while manually creating $model = new UserModel() works, it’s usually unnecessary boilerplate in a Laravel context. The preferred methods are:

  1. Direct Static Call: For simple Model interactions, calling methods directly on the class (e.g., UserModel::methodName($id)).
  2. Dependency Injection: For complex logic or services, inject the required Model via the constructor to ensure your controller remains decoupled and highly testable.

By adopting these patterns, you write code that is not only functional but also adheres to the architectural standards that make Laravel such a powerful framework.