Cookie Consent by Free Privacy Policy Generator ๐Ÿ“Œ Mastering Global Functions in Laravel: Easy Methods for Versions 8,9,10,11.

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š Mastering Global Functions in Laravel: Easy Methods for Versions 8,9,10,11.


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

If you've ever found yourself seeking for efficient ways to have global functions in your Laravel apps you're in the right place. Global functions offer a convenient way to enhance the functionality and productivity of your Laravel apps as you only have to write a function once and have access to it in any part of your app, in this comprehensive tutorial, we'll explore the easiest methods to achieve this. Whether you're a seasoned Laravel developer or just starting your journey, this guide will equip you with the knowledge and tools to leverage global functions effectively, making your development experience smoother and more efficient. Let's dive in!

For this example, we'll be displaying the user count on different pages, it may not a very good example but it would give you an idea on how to implement such functionality in you own app. Let's dive in!

Get your Laravel App running.

In this step we will install a fresh Laravel project using the Laravel installer, you can also use composer or any other means you use to get your Laravel applications up and running.

As at the time of writing this post, Laravel is on version 11. You have to make sure you have a PHP version of at least 8.2. We will now create a Laravel project with the new command:

laravel new command

I would not be using any starter kit for this example.

After installation, let's open the project on our code editor, I'm using Virtual Studio Code.

Project setup in VSCode

Serve the application and open on the browser:

php artisan serve

bash

Served application

Let's get to the main job!

Now that our Laravel app is running, we can now start writing code. For the sake of this example, I'll populate the database with 35 users using the default UserFactory that comes with every Laravel installation. I'll use tinker for ease.

tinker session

After that let's first display the users count on the home page, let's delete everything on the welcome.blade.php file and write this:

Total users: {{ $users }}

While on the web.php file, we do this:

<?php

use App\Models\User;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome', [
        'users' => User::count()
    ]);
});

Our home page should look like this now:

home page

We can now see that if we want to see a count of users on different pages we would have to repeat this line of code on both the view file and route file, this is a lot of work. Let's create a Global Function. There are so many ways to create a global function in Laravel but the best way is using a Service Provider. We'll create it through the terminal, you can name it what ever you want but I'll call mine GlobalFunctionsServiceProvider

php artisan make:provider GlobalFunctionsServiceProvider

This command creates a file on the App\Providers directory. Note that in Laravel 11, you do not need to register this provider in the app.php providers array since it is auto-discovered by Laravel but if you're using a lower version of Laravel you would need to do so. Just open the Config/app.php file and add it to the providers array like this:

'providers' => ServiceProvider::defaultProviders()->merge([
        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        App\Providers\GlobalFunctionsServiceProvider::class, //our custom provider
    ])->toArray(),

Note: You do not need the above step if you're in Laravel 11

Now let's create a file to store our functions, I'll create mine in App\Functions\GlobalFunctions.php After that, open your GlobalFunctionsServiceProvider.php file and add the following code in the register method

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class GlobalFunctionsServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        require_once base_path().'/app/Functions/GlobalFunctions.php';
//remeber to point to the path of the file where your functions are
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        //
    }
}

Now let's write our global function. Open your GlobalFunctions.php file and write the following code

<?php

use App\Models\User;

function users_count()
{
    return User::count();
}

We can now remove the users key from our web.php routes file

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

And edit our welcome.blade.php file to use the function we wrote earlier

Total users: {{ users_count() }}

You can see now that we have the same result on the browser! With this approach, you can call the users_count() function from any part of your application, whether view, controller, class, config files, anywhere, and be certain to get the same result.

You can customize the code to fit your use case, this was just an example.

The source code for this example is on this Github repo.

Thank you. Happy coding.

You can follow me on X(former twitter) if you found this article helpful.

...



๐Ÿ“Œ Mastering Global Functions in Laravel: Easy Methods for Versions 8,9,10,11.


๐Ÿ“ˆ 76.76 Punkte

๐Ÿ“Œ Serverless Prey - Serverless Functions For Establishing Reverse Shells To Lambda, Azure Functions, And Google Cloud Functions


๐Ÿ“ˆ 34.46 Punkte

๐Ÿ“Œ Functions of Commercial Bank: Primary Functions and Secondary Functions


๐Ÿ“ˆ 34.46 Punkte

๐Ÿ“Œ Frida-Ios-Hook - A Tool That Helps You Easy Trace Classes, Functions, And Modify The Return Values Of Methods On iOS Platform


๐Ÿ“ˆ 31.39 Punkte

๐Ÿ“Œ Low CVE-2019-17433: Laravel-admin Laravel-admin


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ Low CVE-2019-17494: Laravel-bjyblog project Laravel-bjyblog


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ I Built Laravel 10 Blog with Admin Panel #laravel


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ Customize Error Views in Laravel #laravel #php #laravelhint #thecodeholic


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ Laravel Boiler Template: Elevate Your Laravel Experience


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ Project setup with Laravel Sail - Part 1 | Laravel Social Media Website


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ A guide to feature flags in Laravel using Laravel Pennant


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ Laravel: Pengenalan Fitur Reverb Di Laravel


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ Laravel-10 Laravel-11 comparison, changes


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ I built $7000 Laravel Project #laravel #laravelproject #laraveldeveloper #laravelframework


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ I built $7,000 Laravel Project for YouTube #laravel #laravelproject #laravelframework


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ Deploy Laravel with Github Actions - Part 45 | Laravel Social Media Website


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ Laravel 11 Crash Course - Laravel in 1 hour


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ Laravel 10 + NGINX + PHP-FPM - my ready to use all in one recipe for your Laravel 10 deployment in Docker


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ Laravel - Unlock the Power of Laravel Gates for Simplified Authorization


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ Build and Deploy Real Time Messaging App with Laravel and React (with Laravel Reverb)


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ Review: Mastering Laravel Validation Rules


๐Ÿ“ˆ 25.88 Punkte

๐Ÿ“Œ Mastering Traits: The First Step to Clean & Efficient Laravel Code


๐Ÿ“ˆ 25.88 Punkte

๐Ÿ“Œ Mastering File and Folder Permissions in Laravel Applications


๐Ÿ“ˆ 25.88 Punkte

๐Ÿ“Œ Mastering Vue.js and Laravel: A Comprehensive Guide to Full-Stack Development


๐Ÿ“ˆ 25.88 Punkte

๐Ÿ“Œ Mastering Standalone Jobs in Laravel 11: Organize Large Codebases with Ease


๐Ÿ“ˆ 25.88 Punkte

๐Ÿ“Œ Mastering File Uploads in Laravel 11: A Comprehensive Guide


๐Ÿ“ˆ 25.88 Punkte

๐Ÿ“Œ Managing PHP Versions with Laravel Herd


๐Ÿ“ˆ 25.51 Punkte

๐Ÿ“Œ Ether Easy Converter/Easy Creator/Easy Burner 1.4.24 memory corruption


๐Ÿ“ˆ 24.84 Punkte

๐Ÿ“Œ Ether Easy Converter/Easy Creator/Easy Burner 1.4.24 Pufferรผberlauf


๐Ÿ“ˆ 24.84 Punkte

๐Ÿ“Œ Mastering JavaScript: A Comprehensive Guide to Essential Methods and Latest Features


๐Ÿ“ˆ 23.85 Punkte

๐Ÿ“Œ Mastering data with 7 new JavaScript Set methods


๐Ÿ“ˆ 23.85 Punkte

๐Ÿ“Œ From Probabilistic to Predictive: Methods for Mastering Customer Lifetime Value


๐Ÿ“ˆ 23.85 Punkte

๐Ÿ“Œ Mastering the Art of Debugging in Magento 2: Essential Methods for a Smooth eCommerce Experience


๐Ÿ“ˆ 23.85 Punkte

๐Ÿ“Œ Mastering the Art of Debugging JavaScript Functions: Tips and Tricks for Smooth Functionality


๐Ÿ“ˆ 23.71 Punkte











matomo