Run a function or route in whole application in Laravel 5.2

I am trying to create an application where i have a user activity log. I dont want to write a special function in all the pages. I just want to metnion it once and run on all the pages or controller where ever i go. Which keep showing on the header.blade.php. And i want to keep using that log all time.

I have a function with like this.

public function headerLogs()
    {
        $latestActivities = Activity::with('user')->latest()->limit(10)->get();
        return view('layouts.header')->with('logs', $latestActivities);
    }

How can i do that?

Laravel has built-in functionality for that: View Composers. They are what you use if you want some data to be in every view that is loaded (of course you specify which views exactly)

So from the docs we would create a service provider for our view composer:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ActivitiesComposerServiceProvider extends ServiceProvider
{
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function boot()
    {    
        // Using Closure based composers...
        view()->composer('layout.header.activity', function ($view) {

        $latestActivities = Activity::with('user')->latest()->limit(10)->get();

        $view->with('logs', $latestActivities);
        });
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

And then you register your ActivitiesComposerServiceProvider service provider by simply adding it to the providers array in config/app.php.

So now you can simply @include('layout.header.activity') and the logs will show with no extra line of code in your controller or view

Accessing the Current Route, Master, 7.x � 6.x � 5.8 � 5.7 � 5.6 � 5.5 � 5.4 � 5.3, 5.2, 5.1 � 5.0 � 4.2 Route Model Binding; Form Method Spoofing; Accessing The Current Route. Basic Routing. All Laravel routes are defined in the app/Http/routes.php file, which is automatically you to register controller routes without specifying the full App\Http\Controllers � To get started, let's create our first route. In Laravel, the simplest route is a route to a Closure. Pop open the app/routes.php file and add the following route to the bottom of the file: Route::get('users', function() { return 'Users!'; }); Now, if you hit the /users route in your web browser, you should see Users! displayed as the response

In your base controller:

view()->composer('layouts.header', function($view){ //your code for header logs which gives $logActivites. $view->with('logs', $logActivites); }

So, whenever your view layouts.header will be created, this $logActivites will be available.

Routing - Laravel, Master, 7.x, 6.x � 5.8 � 5.7 � 5.6 � 5.5 � 5.4 � 5.3 � 5.2 � 5.1 � 5.0 � 4.2 Fallback Routes; Rate Limiting; Form Method Spoofing; Accessing The Current you to register controller routes without specifying the full App\Http\Controllers namespace prefix. The subdomain may be specified by calling the domain method before� Route::get('foo', function { return 'Hello World'; }); The Default Route Files. All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. The routes/web.php file defines routes that are for your web interface.

Everyone had a situation when one variable had to be accessible in every view (blade) file. One possible solution – letting the service provider load our global view information. For Example, type this in your service provider.

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Using view composer for specific view
        view()->composer('welcome',function($view) {
            $view->with('latestActivities', Activity::with('user')->latest()->limit(10)->get());
        });
    }
}

Then you can access the latestActivities variable from all you view files

HTTP Routing - Laravel, Master, 7.x � 6.x � 5.8 � 5.7 � 5.6 � 5.5 � 5.4 � 5.3 � 5.2 � 5.1, 5.0, 4.2 Basic Routing; CSRF Protection; Method Spoofing; Route Parameters You will define most of the routes for your application in the app/Http/routes.php file, you to register controller routes without specifying the full App\Http\Controllers namespace prefix . Form in Laravel 5.2 does not find route. 0. delete function Laravel 5.2. Hot Network Questions Truncation of Napier’s number (e) to an amount of decimal digits

Controller Actions, Most of the routes for your application will be defined in the app/routes.php file. to generate URLs to your routes, you may do so using the URL::to method: is considered the response to the request and the route will not execute. You may also specify that a filter applies to an entire set of routes based on their URI. Here we will create a simple Create Read Update Delete (CRUD) application with Laravel 5.2. Following steps help to beginners for implement Create Read Update Delete (CRUD) procedures in Laravel 5.2. CRUD Operations contains, Add a new Form and insert data into the database. Get whole data from the database and show on the view page.

Controllers - Laravel, Master, 7.x � 6.x � 5.8 � 5.7 � 5.6 � 5.5 � 5.4 � 5.3 � 5.2, 5.1, 5.0 � 4.2 Route Model Binding; Form Method Spoofing; Throwing 404 Errors You may generate URLs to your application's routes using the url helper: group, allowing you to register controller routes without specifying the full App\Http\Controllers namespace prefix . The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything!

Basic Task List - Laravel, Instead of defining all of your request handling logic in a single routes.php file, you may wish Controllers are typically stored in the app/Http/Controllers directory. It is very important to note that we did not need to specify the full controller the name of the controller action being run using the currentRouteAction method: Creating Jobs. Generating Job Classes. By default, all of the queueable jobs for your application are stored in the app/Jobs directory. If the app/Jobs directory doesn't exist, it will be created when you run the make:job Artisan command.

Comments
  • Paste your code in helper.php and auto load in composer. Or use can use trait
  • Yes i have this code in my helper.php. How can i use trait?
  • I guess view::composer is one option? Right?
  • I just did the way you mentioned here. Composer is the way to do this. But i am not getting why its throughing error like this: Undefined variable: logs (View: /Applications/MAMP/htdocs/laravelCRM/resources/views/layouts/header.blade.php) I have also registered my composer with name HeaderLog in my app.php file, here is how i did it register App\Providers\HeaderLogs::class,
  • Did you put the correct name for the view in your composer? My code example has layout.header.activity, yours may be named differently view()->composer('layout.header.activity').
  • Yes i made it, layout.header so whole header can have access to that. I didnt made different file for activity.
  • This problem i am finding in only Closure approach. Rest if i mentioned it like this view()->composer( 'layouts.header', 'App\Http\ViewComposers\HeaderComposer' ); then its working.
  • OK got the issue :) i was making mistake in layout.header it should be layouts :)
  • Sorry I am using from mobile not able to use proper tools to render the code.
  • Sure, your approch is completely correct. But i dont know why i am not able to get it done. with the clouser approch.