Skip to content

Instantly share code, notes, and snippets.

@dillinghamio
Last active January 18, 2019 08:14
Show Gist options
  • Select an option

  • Save dillinghamio/027563b477f5e581253e90aff2f1a595 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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

mrcave commented Nov 22, 2016

Copy link
Copy Markdown

@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

obaid commented Apr 11, 2017

Copy link
Copy Markdown

Where would you place this helper method?

@hassanuos

Copy link
Copy Markdown

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

@thiskbj

thiskbj commented Aug 28, 2018

Copy link
Copy Markdown

+1 for wondering where you put it.

@dillingham

Copy link
Copy Markdown

@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