Skip to content

Instantly share code, notes, and snippets.

@dillinghamio
Last active January 18, 2019 08:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dillinghamio/027563b477f5e581253e90aff2f1a595 to your computer and use it in GitHub Desktop.
Save dillinghamio/027563b477f5e581253e90aff2f1a595 to your computer and use it in GitHub Desktop.
Notification Helper For Laravel Spark

notification Helper For Laravel Spark

The method assumes the current authenticated user, so you only need to pass the message

function notification($message)
{
	$notification = app('Laravel\Spark\Contracts\Repositories\NotificationRepository');
	
	return $notification->create(auth()->user(), $message);
}
And call it like so
notification([
    'icon' => 'fa-users',
    'body' => "Hello World",
    'action_text' => 'Click Me',
    'action_url' => "http://google.com",
]);
@phroggyy
Copy link

How about accepting a second argument for an optional user id? In my case, I would like to notify the team owner when the billing is updated (per user team billing)

@mrcave
Copy link

mrcave commented Nov 22, 2016

@phroggy You've probably sorted this out by now but for anyone else who's just come across this gist and wondered the same thing:

function notification($message, $user = NULL)
{
    $notification = app('Laravel\Spark\Contracts\Repositories\NotificationRepository');

    if(!isset($user)) 
    {
        $user = auth()->user();
    }

    return $notification->create($user, $message);
}

Works exactly as original (notifies current user) unless you specify the optional $user parameter. The $user parameter should be a User object, not just the user ID.

@obaid
Copy link

obaid commented Apr 11, 2017

Where would you place this helper method?

@hassanuos
Copy link

@obaid! Great question. I was thinking the same...

@thiskbj
Copy link

thiskbj commented Aug 28, 2018

+1 for wondering where you put it.

@dillingham
Copy link

@katieben, and others, you can add a helpers.php file to composer and use it

https://laravel-news.com/creating-helpers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment