Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events do not fire when Nova UI is used. #16

Open
greggh opened this issue Jan 21, 2019 · 22 comments
Open

Events do not fire when Nova UI is used. #16

greggh opened this issue Jan 21, 2019 · 22 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@greggh
Copy link

greggh commented Jan 21, 2019

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.

@chelout
Copy link
Owner

chelout commented Jan 22, 2019

I'd like to help you, but i don't have Nova license 😔

@JT501
Copy link

JT501 commented Feb 8, 2019

Hi, I got the same issues when using laravel-admin which is a CMS framework similar to Nova. Although I don't have a Nova license, I want to share my experience to fix this issues in laravel-admin and hope this may help you to find out the problem in Nova.

After few hours digging in laravel-admin source code, I found out the function which update relations and in this function, there is a switch() closure as below:

.
.
.
switch (get_class($relation)) {
                case Relations\BelongsToMany::class:
                case Relations\MorphToMany::class:
                    if (isset($prepared[$name])) {
                        $relation->sync($prepared[$name]);
                    }
                    break;
                case Relations\HasOne::class:
.
.
.

The problem is get_class($relation). When using laravel-relationship-events package, it will return Chelout\RelationshipEvents\BelongsToMany instead of Relations\BelongsToMany, although Chelout\RelationshipEvents\BelongsToMany inherits Relations\BelongsToMany, it still can't match any of the cases, thus no any update (sync in my case) and event fire.

So I change the logic a little bit, and it works prefectly. And I created a pull request to laravel-admin.

.
.
.
 switch (true) {
                case $relation instanceof Relations\BelongsToMany:
                case $relation instanceof Relations\MorphToMany:
                    if (isset($prepared[$name])) {
                        $relation->sync($prepared[$name]);
                    }
                    break;
                case $relation instanceof Relations\HasOne:
.
.
.

At this moment, I don't have any idea without changing the source code of laravel-admin.
If @chelout find out any workaround, it would be great ! 😄

@chelout
Copy link
Owner

chelout commented Feb 8, 2019

Thanks for share @hallelujahbaby!
But without Nova code i can't fix the problem 😢
Maybe @themsaid can help with a dev license? 😬

@greggh
Copy link
Author

greggh commented Feb 11, 2019

@themsaid I'm sure you get requests all the time, but this would be amazing. None of the existing packages I can find actually fire these events when Nova is used. If @chelout had a license it could be fixed in this one. Would be a huge help.

@chelout chelout added enhancement New feature or request help wanted Extra attention is needed labels Feb 22, 2019
@f-liva
Copy link

f-liva commented Feb 22, 2019

I see Nova using a Illuminate\Database\Eloquent\Relations\Pivot instance to do the pivot rescue. I guess that's why events don't get launched.

However, the events at the pivot update don't work that way either:

 Location::find(20993)->products()->first()->save();

Why is the event belongsToManyUpdatingExistingPivot not raised in this case?

@arall
Copy link
Contributor

arall commented Mar 4, 2019

If it helps, this is what the Nova controller does when attaching a model into a morphedByMany relationship.

($pivot = $relationship->newPivot())->forceFill([
    $relationship->getForeignPivotKeyName() => $request->resourceId,
    $relationship->getRelatedPivotKeyName() => $request->input($request->relatedResource),
    $relationship->getMorphType() => $request->findModelOrFail()->{$request->viaRelationship}()->getMorphClass(),
        ]);

@f-liva
Copy link

f-liva commented Apr 16, 2019

Any news here?

@chelout
Copy link
Owner

chelout commented May 7, 2019

As i can see from comments above Nova works with pivot models and doesn't support normal relations, so many-to-many relations can't be supported by this package. Ofcourse we can change basic behavior of laravel's relations, but i don't want to miss miss compatibility.

@f-liva
Copy link

f-liva commented May 7, 2019

How would you do that?

@arall
Copy link
Contributor

arall commented May 7, 2019

Maybe we can implement an adaptor or a Nova component for doing that without messing with this repo? But I have no much clue of how to do it...

@chelout
Copy link
Owner

chelout commented May 7, 2019

@fede91it I guess, it is possible to override laravel's methods like attach, detach, etc to use pivot models.
@arall yeah, i guess we can try to write a component for Nova, but as i mentioned above i don't have license or even Nova code to do it.

@arall
Copy link
Contributor

arall commented May 7, 2019

@chelout I might be able to provide you a license. If you're interested, could you reach me on Telegram? I'm using the same username as in here. Or email (my full name @ gmail.com).

@f-liva
Copy link

f-liva commented May 9, 2019

If necessary, I can sponsor the development of this additional feature.

@chelout
Copy link
Owner

chelout commented May 10, 2019

@fede91it Actually i tend not to change default logic of laravel's relation system, i tend to making Nova plug-in to support our package. If you would like to sponsor this package you can always reach me on telegram @slaffka_moscow

@f-liva
Copy link

f-liva commented May 11, 2019

Why we can't create a trait for the pivot models?

@arall
Copy link
Contributor

arall commented Nov 6, 2019

Any updates on this?

@gavinhewitt
Copy link

Came here because I was looking for the same functionality. Currently working on my Nova admin and this would solve a big issue.

@chelout
Copy link
Owner

chelout commented Nov 15, 2019

There are no updates on this topic. Sorry, guys.

@arall
Copy link
Contributor

arall commented Aug 7, 2020

I did a proof of concept using Laravel native Pivot models events and works fine with Nova: https://github.com/arall/laravel-relationhip-events

@chelout
Copy link
Owner

chelout commented Aug 7, 2020

@arall yes, you right, it's possible with pivot events, but this package doesn't use them.

@riptin
Copy link

riptin commented Mar 24, 2021

bump?

@mostafaznv
Copy link

mostafaznv commented Aug 18, 2021

This is my workaround for this issue. hope it helps.

First, you should create a model for pivot table.

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphPivot;

class Taggable extends MorphPivot
{
    protected $table = 'taggables';
    public $incrementing = true;
}

Then, it's time to define this model to your morph relations

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

class Tag extends Model
{
    public function products(): MorphToMany
    {
        return $this->morphedByMany(Product::class, 'taggable')->using(Taggable::class);
    }
}

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

class Product extends Model
{
    public function tags(): MorphToMany
    {
        return $this->morphToMany(Tag::class, 'taggable')
            ->using(Taggable::class)
            ->withTimestamps();
    }
}

now you can create an observer for your Taggable model and it works perfectly fine with nova admin panel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

8 participants