Laravel Varbox 2.x - duplicate model with relationships included - laravel

I’m using the HasDuplicates trait on my Post custom entity.
The Post has 2 relations:
has many comments
has one author
How can I configure the duplicate functionality in order to duplicate a post record along with its relationships: comments and author?
I see in your documentation that I have the option of excluding relations, but not to include them.

The Varbox\Traits\HasDuplicates automatically duplicates all your eloquent model relationships by default, so that's why there's no option to include any relationships to be duplicated, because they all are duplicated by default.
Also, in the event where you don't want certain relations duplicated (such as belongs to relations), you have the option to exclude them (as you already stated): https://varbox.io/docs/2.x/duplicate-records#exclude-relations
So to answer your questions, you don't need to do anything to include your comments and author relations into the duplication functionality, as they will be included by default.
Suggestion: Depending on your database structure and logic architecture, I think you should consider converting the author relation into a belongs to, instead of has one, but that's up to you.

Related

Querying polymorphic relation with different deepths in Laravel

I have a Laravel 8 application in which I designed two models, Workspace and Section. Every section belong to a workspace, in a one-to-many relationship.
Both sections and workspaces can have comments; they are implemented in a third model, Comment, with a morphTo relationship. In particular, every comment belongs to a commentable, i.e. a Workspace or a Section.
I'm looking for properly defining a relationship which, from Workspace, allow to retrive all the related comments, i.e. both workspace comments and comments of sections belonging to the workspace.
Can I define somehow this kind of relationship? In particular, I would hypothetically define it a proper way, such that I can use commands like Workspace::with('all_comments') and $workspace->load('all_comments').
Laravel seems not offer a standard interface for defining custom relationships
Thanks,

Exclude objects from all collections/queries unless explicitly asked for

I'll have some items in a model's database table that I more often that not won't want to include in queries for that model. So, rather than querying to exclude these items everywhere I call for the model, either directly or via a relationship, it would be nice to tell Laravel 'in one place' to exclude these items from all collections. The criteria for excluding will be a column value.
Perhaps somewhere in the model I can put this criteria?
Ideally the solution will also provide a way to easily explicitly re-include those excluded items in collections, at the point of querying.
Laravel's model scopes are almost there, but I need it the over way around. Perhaps scopes will solve the second part of my quest (in the paragraph above this one).
I found the answer: Global Scopes. https://laravel.com/docs/8.x/eloquent#global-scopes
I was previously looking at an older Laravel version's doc, which didn't have Global Scopes.

My relationships are missing in strapi model afterCreate

I have created a model with a relationship to another model.
I want to send an email with the entry information, afterCreate seems like the place to do so. But it doesn't contain my relationship fields on the model object.
We have an issue about that on GitHub, there is currently not workaround about that.
Alex is re-writing the logic behind the life cycle function, so lot of issue should be fixed on that after the release of the new system.
As of Strapi 3.0.0, the model lifecycles have changed and now afterCreate does contain relationships.

How to store log of all changes to Eloquent model in Laravel 5.4?

I want to store changes to all fields in Eloquent model into database.
I can do it using created and updated events but there is a problem with multiple foreign relations (described as separate tables).
Example:
User
login
Roles -> hasMany
When I update login field it is easy to write old and new value into database, but when I update Roles relation nothing happens.
How can I track all foreign relations (like hasMany, hasManyThrough, ManyToMany)?
Owen-it has a very nice Library called laravel-auditing, which keeps an easy to query list of all changes that are made to a model, and I think it does quite an awesome piece of work. Have used it and it is worth it to try out.
There is no embedded and simple method to do it.
Eloquent doesn't provide any method to implement observers on related models by design. Many proposal in this way have been rejected by Taylor (just one example).
The only thing you can do, is to create your own methods to do it.
You have many possibilities, here are some of them in order of complexity (some of them are "dirty" :-)
add a created and updated observer on each related model
override the save() or create your own saveAndFire() method on your eloquent instances, and from that method retrieve the parent and call its log methods before saving. (this is a little bit "dirty" imho)
encapsulate all your persistence layer and fire events yourself on saving objects (look at the repository pattern, for example)

doctrine2 polymorphic reference

I have a "Post" entity and I want users to vote for those posts. Votes by authenticated and anonymous users are being stored in separate DB tables, so there are two separate "VoteAnonymous" and "VoteAuthenticated" entities which implement the same interface.
Now I've got problem with defining a reference in a "Post" entity and its "targetEntity" option. I wonder if there is any way Doctrine2 could pick one of the polymorphic classes as its field's target entity.
Thanks for any help.
P.S. I'm not able to redesign the DB, there's a ton of legacy code that lies upon this data structure.
Doctrine supports inheritance, so you should create two different entities that share a common parrent, say AbstractVote, which defines all properties.
See this answer - it contains an example of such structure.

Resources