MorphToMany relation empty in `created` Model Observer - laravel

I am using Laravel Nova and have an Entry model that uses spatie/nova-tags-field. (Under the hood this package uses a morphToMany relation between models and tags via a trait)
I attached a model observer on the created event.
I want to access the entry's tags in the observer like this:
public function created(Entry $entry)
{
$tags = $entry->tags;
}
But $tags is always an empty array [], yet I can access the models tags later (not in the observer, but anywhere else) using Entry::find($id)->tags. My guess is that the morphToMany pivot table entry for the attached tags is being created after the observer fires?
Thank for your input.

Because the model hasn't been created yet, try the created method in your observer instead of creating

You could override the attachTags method in your Entry and fire a custom event when $this->wasRecentlyCreated is true.
Also you could listen to the created event (or any other) of your own tag model.
In combination with $touches something close to your desired behaviour may be possible. But obviously the created event of your Entry will always be fired before any tags are attached.

Related

Laravel observer pattern for keeping two related models in sync

I'm using Laravel 5.4. I have two related eloquent models:
ImageFile and ClipartImage.
ClipartImage contains a belongsTo relationship as follows:
class ClipartImage extends Model
{
public function image_file()
{
return $this->belongsTo(ImageFile::class, 'image_file_id');
}
public function promote()
{
$this->image_file->promote();
}
}
On ClipartImage there is a "promote" method which will pass the promote call through to the ImageFile object as it knows how to promote itself.
The problem I'm running into is if properties change on ImageFile as a result of the method call, there are a couple of properties on the related ClipartImage which may need to be updated as a result. In the ImageFile class I could search the database for any related ClipartImages and update the relevant fields, but this doesn't update the existing ClipartImage instance that I am working with. What's the normal way to solve this sort of dependency between models and keeping the object instances in memory in sync?
I guess I'm in need of some sort of Observer pattern but in Laravel these things seem to be handled at the static/class level not for individual object instances - ie I could create an observer class that looked for any changes to ImageFile models, but that isn't going to help me refresh or update my existing ClipartImage object.
Edit: I've used a single "promote" method to illustrate, but there's a bunch of interaction between ClipartImage and the ImageFile class and it's not obvious from the point of view of ClipartImage when it may need to refresh or update itself. Ideally ImageFile would inform it when key fields have changed so it can respond accordingly.

How to check if model's attached items have changed?

Suppose we have Class and Student models with many-to-many relationship defined between them.
I want to perform some action, if $students, that are attached to a $class, change. For example:
$class->attach($newSetOfStudents); // notify a teacher about the change behind the scenes
$class->save(); // by overriden save() or attach() methods
How can this be accomplished? I tried to use ..->isDirty() when overriding the save() method, but it does not seem to work with attached models.
Update
Checking the pivots for ..->isDirty() doesn't seem to work either:
// ..
foreach($class->students as $student){
if($student->pivot->isDirty()) return true;
}
// ..
the best thing to do, is using beforeSave() method by extending Ardent, there you can handle the case you want, do the changes, let it go to the save method by its own.
take a look at this nice package
You can use Model Event - created on Pivot like this:
use Illuminate\Database\Eloquent\Relations\Pivot;
Pivot::created(function($pivot) {
// Do something here
});
This solutions fires for each pivot & only works in case of attach().
See more about this - here
Hope this helps!

What's the most reliable way to catch saves and deletes in Laravel 5.2?

I need to run some code when one of my models is saved (created/updated) or deleted. What's the best way to do that?
There's three different ways that I'm aware of:
Override the save and delete methods on the model
Add creating/updating/deleting callbacks in the boot method
Bind an observer in the boot method
I haven't seen these compared and contrasted, so I don't know what the differences are. I'm worried that the events won't fire under certain conditions.
For example, in Django, deletes only fire if you delete the models one-by-one, but not in a mass delete.
To be clear, I'm looking for answers that compare and contrast these (or other) methods -- not simply suggest even more ways of doing the same thing.
It's just my opinion for several methods you mention previously.
Override the save and delete methods on the model ( If you override it then next update of Laravel change visibility of method your code does not work again. It would throw Exception or PHP error. You have to modify it to work again )
Add creating/updating/deleting callbacks in the boot method ( exist in Laravel 4 you should check it again in Laravel 5 maybe different implementation using Event and Listener )
Bind an observer in the boot method ( exist in Laravel 4 you should check it again in Laravel 5 maybe different implementation using Event and Listener )
I think you should using Event and Listener provided by Laravel. It maybe still work on next Laravel Update. I assume Event and Listener as minor change area in Laravel and changed maybe just different method implementation.
Laravel should have plan of development assign which part of Laravel will be developed as major change area ( big modification ) or minor change area ( little modification ). If you try to change or override major change area it would can't be used on next Laravel Update.
You can register Event and Listener for save and delete record. Laravel have fireModelEvent method on Model ( Illuminate\Database\Eloquent\Model ) which trigger specific Laravel Event. If you've registered Event, Dispatcher ( Illuminate\Events\Dispatcher ) will execute Listener of Event.
Documentation about Laravel Events:
https://laravel.com/docs/5.3/events
https://laravel.com/docs/5.2/events
I assume you have YourModel as Model then do the following action on the below.
Register Event and Listener. Open app\Providers\EventServiceProvider.php then Add Event and Listener to EventServiceProvider.listen properties for YourModel or follow Laravel Documentation to create event using other way.
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
...
'eloquent.saved: App\YourModel' => [
'App\YourModel#eventSaved',
],
];
}
Adding eventSaved method on App\YourModel as Listener for Event so you can do specific action after save or delete.
class YourModel extends Model
{
public function eventSaved(){
// You can add your code to catch save here
}
}
The three methods and 4th referred by #joko. There may be more as well but lets focus on the 4 methods.
Let me describe you them one by one:
1) Override the save and delete methods on the model
In this method you are using OOPD method overriding. You are overriding Laravel's interal save method and adding your additional code by defining your own save method on top of it. This should be avoided as Laravel keep evolving and it may happen that thing start to fail if major change is done like Suppose in future laravel replace save method with any other method to save the records. Then again you will have to create another method to override that new method. Also writing code here may grow your model class file. You model may keep handling things like he shouldn't handle(Example: Sending Email). This method should be avoided.
2) Add creating/updating/deleting callbacks in the boot method
Here you are defining code on the Boot method of the Model. This method should only be used if there is much little code/things that you need to handle on event. The drawback of this method is that it make code more complicated and cluttered as you may write all logic in one like like functional programming. Suppose if you have to do some stuff on before creating and after created. You boot method will grow.
3) Bind an observer in the boot method
This method is pretty good. You create one observer class which handles such stuff that what should happen on Laravel events. It makes code more cleaner and easy to maintain.
Example: Suppose you have to write code in creating, saving, saved, deleting in these methods. In this case, method 1) and method 2) won't be good practice because in
Method 1: We will have to create this 4 methods and override them as well and support them in future releases of Laravel. In this case, code in your Model will also grow because of overriding this methods
Method 2: In this case your boot method will grow as well so you Model file will become a junk of code.
In method 1 and 2 also remember that its not responsibility of your Model to do many of the stuff that you going to write. Like sending email when user is created. These codes you may end up writing in created method.
Suppose now you have scenario where you need to send email to user on created event as well as you need to make user's entry log user in customer CRM. then you will have to write code for both in same method. Probably, you may not following single responsibility principle there. What should we do in the case? See method 4.
4) Other method suggested by #joko
The scenario that i suggested in method 4's end. You may send email to user and log him in Customer CRM whenever it is created. Then your method will do 2 things(Sending email and logging in CRM). It may not following single responsibility principle. What if better we can decouple both of them. Then comes this method.
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
'eloquent.saved: App\User' => 'App\Listeners\SendWelcomeEmailToUser'
'eloquent.saved: App\User' => 'App\Listeners\LogUserInCRM'
];
}
Create two listener classes:
class SendWelcomeEmailToUser
{
public function handle(User $user){
// Write code to send email
}
}
class LogUserInCRM
{
public function handle(User $user){
// Write code to log
}
}
Through this you can separate out codes and make them more cleaner.
I generally prefer this method its mode clean. It also gives you much better idea that what actually happen when event happens. It becomes one single point for Event to Listener mapping.
You can create event handlers, for every create/update of model, for example to add to cache the model data which is just saved to database or going to save to database, easier to retrieve without select query call,
while delete call, use forget for given key on cache handler event to delete cache as well as to delete from database too.
I'm partial to doing things manually when you need to know exactly how they're done. I recently used this Laravel Boilerplate to start a project and I like the way they manually fire events in the repository when a model is updated:
https://github.com/rappasoft/laravel-5-boilerplate/blob/master/app/Repositories/Backend/Access/User/EloquentUserRepository.php
Since models should always be updated through the repository, you always get to manually decide how events are handled. You could fire your own event when multiple models are deleted, and act accordingly. All of your options will work though, you just need to find the option that suits your needs best.
You can create abstract Model class that extends Illuminate\Database\Eloquent\Model class and all your model will extend this class. With implementation like this you can have more control on the models. For example
<?php
namespace App\Base\Database;
use Illuminate\Database\Eloquent\Model as BaseModel;
abstract class Model extends BaseModel
{
public function save(array $options = [])
{
//your code here
return parent::save($options);
}
}
You can do this for all the methods of the Model class and also you can add additional methods that are relevant for all models in your application

Manually inject model relation using Eloquent

How can I add a model into the relations array of another model?
E.g.
Domain belongsTo Owner.
Owner hasOne Domain.
I have $domain (instance of Domain).
I have $owner (instance of Owner).
I want to add $domain to $owner->relations[] so that I can just use $owner->domain later on in my code.
The reason for doing this is such that in one particular controller i only need a partial data set from each model so use fluent to query with a join for performance reasons then fill the models.
Then for readability's sake I'd like to use $owner->domain->id etc
$domain->owner()->associate($owner); gives me a $domain->owner option
But then I can't work out the opposite version
$owner->domain()->associate($domain)
$owner->domain()->attach($domain)
both result in the following fatal error
Call to undefined method Illuminate\Database\Query\Builder::[attach|associate] ()
NB: I don't want to save anything as I've already loaded all the data i need.
setRelation() should work. It sets the value in the relations array.
$owner->setRelation('domain', $domain);
When setting a one to many relationship, you may need to use values():
$owner->setRelation('domains', $domains->values());

Observer for Magento Attribute Set Save After, Delete

I need an Observer called when an Attribute Set is Saved & Deleted. I can't seem to find the right event name for it.
After some digging, I figured out that the Model I need to tap into is http://docs.magentocommerce.com/Mage_Eav/Mage_Eav_Model_Entity_Attribute_Set.html
Since it implements the Mage_Core_Model_Abstract class, it should allow me access to the save_after, delete_after event names. But I can't seem to figure out the syntax for defining the observer for it.
I tried eav_attribute_set_save_after, model_eav_attribute_set_save_after, attribute_set_save_after but they don't seem to work.
What's the right event name?
If you had dug just a little bit more, you would have seen that these "generic" model events are formed like this :
Mage::dispatchEvent($this->_eventPrefix.'_save_after', $this->_getEventData());
Now if you look at the Mage_Eav_Model_Entity_Attribute_Set class you see the eventPrefix attribute defined as :
protected $_eventPrefix = 'eav_entity_attribute_set';
So your event will be :
eav_entity_attribute_set_save_after
Note that this protected attribute needs to be defined for each Model you want to access generic events (some magento models doesn't have this attribute setted by default, just be aware of it... you might have to add it yourself someday)

Resources