chelout / laravel-relationship-events Goto Github PK
View Code? Open in Web Editor NEWMissing relationship events for Laravel
License: MIT License
Missing relationship events for Laravel
License: MIT License
This package is great, does everything we need, except in Laravel Nova. When I use the Attach or Detach buttons on the Nova UI for any of my belongsToMany relationships, nothing fires, no events.
The same relationships with no code changes work great outside of Nova, and I get my events in my observer class for. In jobs running on the schedule, and even down in tinker on the command line. The events fire.
public function belongsToManyAttached($relation, Website $website, $ids)
and
public function belongsToManyDetached($relation, Website $website, $ids)
Website is the class I am observing.
Hi,
When I detach
a related model my event only receives 1 argument.
$form->>fields()->detach($id);
my event is called, but the constuctor only receives one argument, the Form
class.
use Chelout\RelationshipEvents\Concerns\HasBelongsToManyEvents;
use Illuminate\Database\Eloquent\Model;
class Form extends Model
{
use HasBelongsToManyEvents;
protected $dispatchesEvents = [
'belongsToManyDetached' => EventDetached::class
];
public function fields()
return $this->belongsToMany(Fields::class):
}
What I'm doing wrong? I tried to set the dispatchable event on the Field
class, but it's never called from there.
i have an issue with defining hasOneCreating method in an observer it is working fine if defined in the boot method of the parent class
Class:
class Payment extends Model
{
use HasOneEvents;
public function transaction()
{
return $this->hasOne(Transaction::class);
}
}
Observer:
class PaymentObserver
{
/**
* Handle the User "hasOneCreating" event.
*
* @param Payment $payment
* @param Transaction $transaction
* @return void
*/
public function hasOneCreating(Payment $payment, Transaction $transaction)
{
dump($transaction);
}
}
AppServiceProvider:
public function boot()
{
Payment::observe(PaymentObserver::class);
}
Laravel version: 7.8.1
package version: 1.2.1
Thanks a lot for this awesome plugin.
I am also using this plugin https://github.com/staudenmeir/eloquent-eager-limit and getting error.
Trait method newMorphToMany has not been applied, because there are collisions with other trait methods on App\Models\Post.
Is there any way to resolve this conflict keeping functionality of both?
I am setting up audit logging and want to record old and new relations of a model:
// Record old values
static::morphToManyAttaching(function($relation, Model $model) {
dump($model->{$relation}->pluck('name')->toArray());
});
// Store activity
static::morphToManyAttached(function($relation, Model $model) {
dump($model->{$relation}->pluck('name')->toArray());
}
The relations returned from the model in morphToManyAttached
are identical to those returned from the model in morphToManyAttaching
.
For example: https://github.com/chelout/laravel-relationship-events/blob/master/doc/7-many-to-many-polymorphic.md
Markdown code:
static::morphedByManyAttached(function ($parent, $ids, $attributes) {
Log::info("Post has been attached to tag {$parent->name}.");
});
Markdown text:
MorphedByMany::attach
I just recently installed Telescope and got an error when inserting belongsToMany
relation.
get_class() expects parameter 1 to be object, string given
Stacktrace:
../vendor/laravel/telescope/src/Watchers/ModelWatcher.php:35
--
../vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:357
../vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:209
../vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:169
../vendor/chelout/laravel-relationship-events/src/Concerns/HasBelongsToManyEvents.php:187
../vendor/chelout/laravel-relationship-events/src/BelongsToMany.php:47
Is there any chance to get this package compatible with telescope?
I recently needed the functionality of this package but with some adjustments. I decided to fork and the rewrite this package with a different set of functionality since this package no longer seems to be under active development.
The main benefit of this new package is that it allows you to fire a different set of events per relationship or not fire events at all for specific events.
For example lets say you have a User model which hasMany Posts and hasMany Comments. With my package you can fire postsCreating, postsCreated, postsSaving, and postsSaved events and not fire any events on the comments relationship.
The package is still in development so I haven't made any official releases of it but I have tested the existing functionality.
Please check it out here: https://github.com/artificertech/laravel-relationship-events
Also big thanks to @chelout and everyone else who contributed to the existing package. I have made sure to mention you as the original package creator in the README
Hands up this will totally be my issue but I can't quite work out why. (new to Laravel).
I've setup both traits on my BookingFamilyMember Model:
use HasBelongsToManyEvents;
use HasRelationshipObservables;
And set the boot on the model as described
static::belongsToManyAttaching(function ($relation, $parent, $ids) {
Log::debug("Attaching to member {$parent->name}. ");
});
And in my BookingFamilyMemberObserver I have this:
public function belongsToManyAttaching($relation,BookingFamilyMember $member,$ids)
{
dump($relation);
}
Now in your example you show $relation being tied to Model.. however if I do that it fails.. My relation return "journeys"
Which is how it's being attached:
$member->journeys()->attach(1);
Is that all good - or am I missing something by loosely attaching it like this for a ManytoMany?
It works.. I just didn't want to miss something obvious if it should be tied to a Model.
Thank you!
is compatibility with owen-it/laravel-auditing?
or what are the steps to make it compatible? Thx
Hi,
I have a User and Permission classes.
User has the Trait: HasMorphToManyEvents.
I added the attached event under boot:
static::morphToManyAttached(function ($relation, $model, $ids, $attributes = []) {
print_r($ids); // includes the added permission
print_r($model->permissions->pluck('id')->toArray()); / doesn't include the permission
});
I think the updated model should be sent to the Event (e.g. static::find($model->id)) instead of passing $model.
Or did I miss anything?
I spent an hour to find out that when using Observer
you need to define extra observable events to the model so that the observer class can listen to those custom events like so:
/**
* These are extra user-defined events observers may subscribe to.
*
* @var array
*/
protected $observables = [
'belongsToManyAttaching',
'belongsToManyAttached',
'belongsToManyDetaching',
'belongsToManyDetached',
];
Please update the docs.
Hello, this package is useful for my project. But, I have a problem in detach
function.
I call detach()
without any parameter to detach large number of ids, which will over 65535 of PDO limit, and want to record their events.
I think $result = parent::detach($ids, $touch)
this can be modified by check the parameters which user pass, like:
public function detach($ids = null, $touch = true)
{
// Check parameters
$sendIds = (! empty($ids));
....
if ($result = parent::detach(($sendIds) ? $ids : null, $touch)) {
....
If there are any further information or solution, please kindly to tell me. Thank you.
Hello, I have a problem with package, i don't know, maybe I'm doing something wrong.
I have bootable trait bootConvertable with HasOneUpdating with anonymous function and two parameters, $parent and $model like in your documentation. When I dumping $model, everything is ok, working, but after change some values in model, this is not saved to the database (with laravel creating/updating etc. is ok). Example:
Method in trait which is used in User model:
protected static function bootConvertable()
{
static::hasOneUpdating(function ($parent, $model) {
$model->account_to_pay = '21122121221212121212121221';
});
}
Parent is User model with relation HasOne to UserProfile model. account_to_pay (is column in UserProfile) is not changed/saved to the database.
Could someone help with that? I don't know if problem is with other relations, I'm trying this relation for now.
When trying to use:
use Chelout\RelationshipEvents\Concerns\HasMorphedByManyEvents;
use Chelout\RelationshipEvents\Concerns\HasMorphToManyEvents;
class Demo extends Model
{
use HasMorphToManyEvents;
use HasMorphedByManyEvents;
}
tells me Trait method 'newMorphToMany' will not be applied because it collides with 'HasMorphToManyEvents'.
Is there an easy work around for this?
Hi @chelout,
Is this compatible with Laravel 5.5 ? or it only works with Laravel 5.6 ?
It looks very nice.
When adding the HasRelationshipObservables trait to multiple models, the tests often fail, because of observers receiving the event twice.
For eg. a user and a group model with HasRelationshipObservables. With two observers that subscribe to belongsToManyAttached both.
trait HasRelationshipObservables
{
/**
* @var array
*/
protected static $relationshipObservables = [];
/**
* Initialize relationship observables.
*
* @return void
*/
public static function bootHasRelationshipObservables()
{
......gets the methods
static::mergeRelationshipObservables($methods);
}
Static properties will most of the time in tests never be reset, because of how PHPUnit works. When booting HasRelationshipObservables
, then it merges the $methods
with the static $relationshipObservables
. This causes the observer to be fire multiple times in tests where the static property is never reset.
A solution would be to set $relationshipObservables to an empty array in the bootHasRelationshipObservables. Anyone expiring the same issues?
Hello,
i have a model with 2 belongsToMany relationships, so this code :
static::belongsToManyDetached(function($relation,$parent,$ids){ });
catch the 2 relationships when detached and i can't figure how to distinguish which one fired the event.
EDIT:
i was expecting $relation to return the class name of the target model, but in my case i get "insertUpdateData" .
Now i think this is the name of a function from a CMS i use that update relationship data directly so the event is not aware of the target model.
So this can be an issue of the CMS.
I can't upgrade to Laravel Framework 5.8 with laravel-relationship-events installed.
HasRelationshipObservables trait doesn't have a Str
class imported, so throws an error as this class is not existing in the package Traits directory. PR #29
Hi,
Thank you for the package... I tried to use it with a multi tenant package and as soon as I do
static::hasManySaved(function ($parent, $related) { dd('hello'); });
I can't even run migrations anymore, it says my database is not configured.
Any idea that can help me to figure out the problem?
Hello, I have a problem with package,When the primary key is UUID, not ID,This can lead to bugs
public function detach($ids = null, $touch = true)
{
// Get detached ids to pass them to event
$ids = $ids ?? $this->parent->{$this->getRelationName()}->pluck('id');
......
return $result;
}
I using this code
protected $dispatchesEvents = [ 'belongsToManyAttached' => HasBelongsToManyEvents::class, ];
And it show exception.
Cannot instantiate trait Chelout\RelationshipEvents\Concerns\HasBelongsToManyEvents
Please help me solve it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.