Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Pass $variables for all functions with constructor in the controller (Laravel)

Define a Variable Construct Method and use it in all Methods in Laravel

Do you want to Define a Variable Construct Method and use it in all Methods in the Laravel application?

Construct Method and use it in all Methods in Laravel
use a variable in all methods of a class by passing the constructor in the Laravel application

Pass $variables for all functions with the constructor in the controller (Laravel)

This step-by-step tutorial helps you learn how to use a variable in all methods of a class by passing the constructor in the Laravel application with the help of the constructor.

Define a variable in the constructor in Laravel

The “__construct()” function is a special method that is automatically called when an object of a class is created. It is commonly used to set up initial values or perform other setup tasks. This function is often used to set up dependency injection or to perform other tasks that need to happen when a class is instantiated in the Laravel application.

Note: If you wanna define a variable once and use many methods of the same controller then use that variable in the constructor by $this keyword.

Use variables across methods of a class in Laravel

To use variables across methods of a class in Laravel, we have some users in our user’s table, in the constructor, we will get all the users, and we will access this variable on our index method.

routes\web.php

<?php

use Illuminate\Support\Facades\Route;


Route::get('/user', [App\Http\Controllers\UserController::class, 'index']);

Route::post('/post-store', [App\Http\Controllers\UserController::class, 'store']);


app\Http\Controllers\UserController.php

<?php

namespace App\Http\Controllers;

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

class UserController extends Controller
{
    public function __construct()
    {
        $this->users = User::get();
    }

    public function index()
    {
        $users = $this->users;
        dd($users);
    }
}

Output:-

Define a Variable in Laravel Construct Method

Get the current auth id in a constructor in Laravel Example

Now, we will define auth in __construct, and we will get auth details in the entire class.

We will get the auth id in our index method, and then we will store some posts in the store method and insert the auth id in the posts table.

By using the __construct we declared the auth only once and we can access the auth details in the entire class in the Laravel application.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Post;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware(function ($request, $next) {
        $this->auth = Auth::user()->id;
        return $next($request);
        });
    }

    public function index()
    {
        $authID = $this->auth;
        // dd($authID);
    }

    public function store(Request $request){
        $post = new Post();
        $post->user_id =  $this->auth;
        $post->title   = $request->title;
        $post->content = $request->content;
        $post->save();
    }
}

Output:-

pass $variables for all functions with constructor in controller (Laravel)

I hope that this article helped you learn How to Define a Variable Construct Method and use it in all Methods in Laravel 9. You may also want to check out our guide on How To Create Factory In Laravel 9 example in the Laravel application.

Read also:- Generate A Unique Mobile Phone Number With Faker In Laravel