Skip to content

Instantly share code, notes, and snippets.

@cpereiraweb
Created November 2, 2016 03:29
Show Gist options
  • Save cpereiraweb/1a709c6078903a659c91a61f44256980 to your computer and use it in GitHub Desktop.
Save cpereiraweb/1a709c6078903a659c91a61f44256980 to your computer and use it in GitHub Desktop.
<?php
namespace SnPortal\Providers;
use SnPortal\Models\Client;
use SnPortal\Observers\ClientObserver;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Client::observe(ClientObserver::class);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
<?php
namespace SnPortal\Observers;
use SnPortal\Models\Client;
class ClientObserver
{
/**
* Listen to the Client creating event.
*
* @param Client $client
* @return void
*/
public function creating(Client $client)
{
$client->created_by = (App::runningInConsole()?1:\Auth::user()->id); // Se estiver rodando via artisan, atribui 1
$client->updated_by = (App::runningInConsole()?1:\Auth::user()->id); // Se estiver rodando via artisan, atribui 1
}
/**
* Listen to the Client updating event.
*
* @param Client $client
* @return void
*/
public function updating(Client $client)
{
$client->updated_by = (App::runningInConsole()?1:\Auth::user()->id); // Se estiver rodando via artisan, atribui 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment