All Projects → chelout → Laravel Relationship Events

chelout / Laravel Relationship Events

Licence: mit
Missing relationship events for Laravel

Projects that are alternatives of or similar to Laravel Relationship Events

Laravel Google Calendar
Manage events on a Google Calendar
Stars: ✭ 787 (+105.48%)
Mutual labels:  events, laravel
Cms
Decoupled CMS for any Laravel app, gain control of: pages, blogs, galleries, events, images, custom modules and more.
Stars: ✭ 498 (+30.03%)
Mutual labels:  events, laravel
Laravel Transactional Events
Transaction-aware Event Dispatcher for Laravel
Stars: ✭ 263 (-31.33%)
Mutual labels:  events, laravel
Laravel Event Projector
Event sourcing for Artisans 📽
Stars: ✭ 650 (+69.71%)
Mutual labels:  events, laravel
Laravel Event Broadcast
Laravel event broadcasting with Node.js, Redis & Socket.io
Stars: ✭ 5 (-98.69%)
Mutual labels:  events, laravel
Rabbitevents
Nuwber's events provide a simple observer implementation, allowing you to listen for various events that occur in your current and another application. For example, if you need to react to some event published from another API.
Stars: ✭ 84 (-78.07%)
Mutual labels:  events, laravel
Attendize
Attendize is an open-source ticket selling and event management platform built on Laravel.
Stars: ✭ 3,285 (+757.7%)
Mutual labels:  events, laravel
Laravel Pt Br Localization
Tradução do Laravel para português brasileiro (pt_BR locale)
Stars: ✭ 373 (-2.61%)
Mutual labels:  laravel
Coastercms
The repository for Coaster CMS (coastercms.org), a full featured, Laravel based Content Management System
Stars: ✭ 380 (-0.78%)
Mutual labels:  laravel
Starter Laravel Angular
Laravel and AngularJS Starter Application Boilerplate featuring Laravel 5.3 and AngularJS 1.5.8
Stars: ✭ 373 (-2.61%)
Mutual labels:  laravel
Venture
Venture allows you to create and manage complex, async workflows in your Laravel apps.
Stars: ✭ 374 (-2.35%)
Mutual labels:  laravel
Laravel Activitylog
Log activity inside your Laravel app
Stars: ✭ 4,123 (+976.5%)
Mutual labels:  laravel
Secure Headers
PHP Secure Headers
Stars: ✭ 379 (-1.04%)
Mutual labels:  laravel
Laravel Validation Rules
A set of useful Laravel validation rules
Stars: ✭ 374 (-2.35%)
Mutual labels:  laravel
Eloquent Taggable
Easily add the ability to tag your Eloquent models in Laravel.
Stars: ✭ 382 (-0.26%)
Mutual labels:  laravel
Laravel
[DEPRECATED] See https://github.com/lucidarch/lucid
Stars: ✭ 373 (-2.61%)
Mutual labels:  laravel
Awesome Cybersecurity Datasets
A curated list of amazingly awesome Cybersecurity datasets
Stars: ✭ 380 (-0.78%)
Mutual labels:  events
Laravel Flash
A lightweight package to flash messages
Stars: ✭ 382 (-0.26%)
Mutual labels:  laravel
Laravel Missing Page Redirector
Redirect missing pages in your Laravel application
Stars: ✭ 378 (-1.31%)
Mutual labels:  laravel
Laravel Firebase
A Laravel package for the Firebase PHP Admin SDK
Stars: ✭ 369 (-3.66%)
Mutual labels:  laravel

Laravel Relationship Events

Missing relationship events for Laravel

Build Status Total Downloads Latest Stable Version License

Install

  1. Install package with composer

Stable branch:

composer require chelout/laravel-relationship-events

Development branch:

composer require chelout/laravel-relationship-events:dev-master
  1. Use necessary trait in your model.

Available traits:

  • HasOneEvents
  • HasBelongsToEvents
  • HasManyEvents
  • HasBelongsToManyEvents
  • HasMorphOneEvents
  • HasMorphToEvents
  • HasMorphManyEvents
  • HasMorphToManyEvents
  • HasMorphedByManyEvents
use Chelout\RelationshipEvents\Concerns\HasOneEvents;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasOneEvents;

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

        /**
         * One To One Relationship Events
         */
        static::hasOneSaved(function ($parent, $related) {
            dump('hasOneSaved', $parent, $related);
        });

        static::hasOneUpdated(function ($parent, $related) {
            dump('hasOneUpdated', $parent, $related);
        });
    }

}
use Chelout\RelationshipEvents\Concerns\HasMorphToManyEvents;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasMorphToManyEvents;

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

        /**
         * Many To Many Polymorphic Relations Events.
         */
        static::morphToManyAttached(function ($relation, $parent, $ids, $attributes) {
            dump('morphToManyAttached', $relation, $parent, $ids, $attributes);
        });

        static::morphToManyDetached(function ($relation, $parent, $ids) {
            dump('morphToManyDetached', $relation, $parent, $ids);
        });
    }

    public function tags()
    {
        return $this->morphToMany(Tag::class, 'taggable');
    }

}
  1. Dispatchable relationship events. It is possible to fire event classes via $dispatchesEvents properties and adding HasDispatchableEvents trait:
use Chelout\RelationshipEvents\Concerns\HasOneEvents;
use Chelout\RelationshipEvents\Traits\HasDispatchableEvents;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasDispatchableEvents;
    use HasOneEvents;

    protected $dispatchesEvents = [
        'hasOneSaved' => HasOneSaved::class,
    ];

}

Relationships

Observers

Starting from v0.4 it is possible to use relationship events in Laravel observers classes Usage is very simple. Let's take User and Profile classes from One To One Relations, add HasRelationshipObservables trait to User class. Define observer class:

namespace App\Observer;

class UserObserver
{
    /**
     * Handle the User "hasOneCreating" event.
     *
     * @param \App\Models\User $user
     * @param \Illuminate\Database\Eloquent\Model $related
     *
     * @return void
     */
    public function hasOneCreating(User $user, Model $related)
    {
        Log::info("Creating profile for user {$related->name}.");
    }

    /**
     * Handle the User "hasOneCreated" event.
     *
     * @param \App\Models\User $user
     * @param \Illuminate\Database\Eloquent\Model $related
     *
     * @return void
     */
    public function hasOneCreated(User $user, Model $related)
    {
        Log::info("Profile for user {$related->name} has been created.");
    }
}

Don't forget to register an observer in the boot method of your AppServiceProvider:

namespace App\Providers;

use App\Models\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
// ...
    public function boot()
    {
        User::observe(UserObserver::class);
    }
// ...
}

And now just create profile for user:

// ...
$user = factory(User::class)->create([
    'name' => 'John Smith',
]);

// Create profile and assosiate it with user
// This will fire two events hasOneCreating, hasOneCreated
$user->profile()->create([
    'phone' => '8-800-123-45-67',
    'email' => '[email protected]',
    'address' => 'One Infinite Loop Cupertino, CA 95014',
]);
// ...

Todo

  • Tests, tests, tests
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].