Support the ongoing development of Laravel.io →
posted 10 years ago
IOC

It works


<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Test extends Model{

	protected $fillable = ['text'];	

	public static function boot()
    {
        parent::boot();

        parent::observe(new \App\Http\Observers\TestObserver);
    }

}

But when i put this code on a Service Provider... dont works...

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;


class TesteServiceProvider extends ServiceProvider {

	/**
	 * Bootstrap the application services.
	 *
	 * @return void
	 */
	public function boot()
	{
		\App\Test::observe(new \App\Http\Observers\TestObserver);
		
	}

	/**
	 * Register the application services.
	 *
	 * @return void
	 */
	public function register()
	{
		//
	}

}

my app.php

'providers' => [

		/*
		 * Application Service Providers...
		 */
		'App\Providers\AppServiceProvider',
		'App\Providers\EventServiceProvider',
		'App\Providers\RouteServiceProvider',
		'App\Providers\CourseServiceProvider',		
		'App\Providers\TestServiceProvider',

Someone knows why?

Last updated 3 years ago.
0

The resolution I found was to create an ObserverServiceProvider and place it after DatabaseServiceProvider in the config/app.php file.

Because the event dispatcher is attached to the base Model class in DatabaseServiceProvider. Once event is dispatched in DatabaseServiceProvider we can use Observer.

0

Thanks, saved me a good amount of more debugging.

0

You looked at the event solution that comes as part of L5?

https://laracasts.com/lessons/laravel-5-events

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.