korridor / laravel-has-many-merged Goto Github PK
View Code? Open in Web Editor NEWCustom relationship for Eloquent that merges/combines multiple one-to-may (hasMany) relationships
License: MIT License
Custom relationship for Eloquent that merges/combines multiple one-to-may (hasMany) relationships
License: MIT License
Hi, I was wondering if by any change you could add support for inverse relationships. Same functionality but from the other side of the relartion. Thanks!
In my application,
I have MessageThread
and Message
model.
This is the migration for message_threads
table:
$table->string('sender_number', 15);
$table->string('receiver_number', 15);
$table->string('sender_number_receiver_number', 30)->virtualAs('CONCAT(`sender_number`, "-", `receiver_number`)');
$table->string('receiver_number', 30)->virtualAs('CONCAT(`receiver_number`, "-", `sender_number`)');
$table->string('contact_name', 75)->nullable();
$table->unique(['sender_number', 'receiver_number']);
And messages
$table->string('from', 15);
$table->string('to', 15);
$table->string('from_to', 30)->virtualAs('CONCAT(`from`, "-", `to`)');
$table->string('to_from', 30)->virtualAs('CONCAT(`to`, "-", `from`)');
$table->text('text')->nullable();
With that, I have relations like this in MessageThread
:
public function messages(): HasManyMerged
{
return $this->hasManyMerged(Message::class, ['from_to', 'to_from'], 'sender_number_receiver_number');
}
It works fine, I mean I can get all messages (incoming, outgoing) from a message thread.
But I want to get a list of Threads, based on Messages.
Like, Thread which has most recent message should be on top, and least recent thread to the bottom.
I'm sure it's not the best solution for messaging, seems too hacky, but this is what I could come up with.
Please suggest some way to sort the threads, or maybe suggest something different all together.
Thanks.
What I did : composer require korridor/laravel-has-many-merged "^0"
What happened :
./composer.json has been updated
Running composer update korridor/laravel-has-many-merged
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Root composer.json requires korridor/laravel-has-many-merged == 0.0.0.0, found korridor/laravel-has-many-merged[dev-feature/laravel_10, dev-main, 0.0.1, 0.0.2, 0.0.3, 1.0.0] but it does not match the constraint.
Installation failed, reverting ./composer.json and ./composer.lock to their original content.
Your help would be greatly appreciated!
Everything's Great ๐
"with" method is working but only problem i know is that "withCount" method doesn't working
hi, it is possible to support merging two relation BelongsToMany and hasMany and returning relation laravel.
I need to merge two relationships (take and merge records from one table using different relationships...) and return the relationship in order to be able to perform actions with them... and then the method ->paginate($perPage, ['*'], 'page', $pageCurrent);
For example
There are item and seller models
seller.php
public function items()
{
return $this->belongsToMany(Item::class, 'item_seller', 'seller_id', 'item_id')->withPivot('id', 'enabled');
}
public function myItems()
{
return $this->hasMany(Item::class, 'seller_id');
}
I need to combine these two relations and return something similar
public function ALL_items_merged()
{
return $this->myItems() + $this-> items()
}
$seller->ALL_items_merged()
for further work with this relationship
Thanks for any tips or solutions
It would be very nice to have HasOne support... nice work!
Hello, trying to use this for shops...
This is my model:
class Shop extends Model
{
use HasFactory, SoftDeletes, HasManyMergedRelation;
public function locations(): HasManyMerged
{
return $this->hasManyMerged(ShopAddress::class, ShopNumber::class);
}
public function addresses(): HasMany
{
return $this->hasMany(ShopAddress::class, 'shop_id');
}
public function numbers(): HasMany
{
return $this->hasMany(ShopNumber::class, 'shop_id');
}
}
How would I make locations relationship work??
any chance of this working where the field is json data? for example {"email":"[email protected]"}?
Would it be hard to achieve Laravel 10 support?
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.