GithubHelp home page GithubHelp logo

korridor / laravel-has-many-merged Goto Github PK

View Code? Open in Web Editor NEW
72.0 4.0 4.0 31 KB

Custom relationship for Eloquent that merges/combines multiple one-to-may (hasMany) relationships

License: MIT License

PHP 97.57% Dockerfile 2.43%
laravel php eloquent laravel-package laravel-eloquent

laravel-has-many-merged's Introduction

Laravel has many merged

Latest Version on Packagist License GitHub Workflow Lint GitHub Workflow Tests Codecov

Custom relationship for Eloquent that merges/combines multiple one-to-may (hasMany) relationships. This relation fully supports lazy and eager loading.

Note

Check out solidtime - The modern Open Source Time-Tracker at solidtime.io

Installation

You can install the package via composer with following command:

composer require korridor/laravel-has-many-merged

If you want to use this package with older Laravel/PHP version please install the 0.* version.

composer require korridor/laravel-has-many-merged "^0"

Requirements

This package is tested for the following Laravel versions:

  • 10.* (PHP 8.1, 8.2, 8.3)
  • 11.* (PHP 8.2, 8.3)

Usage examples

In the following example there are two models User and Message. Each message has a sender and a receiver. The User model has two hasMany relations - one for the sent messages and the other for the received ones.

With this plugin you can add a relation that contains sent and received messages of a user.

use Korridor\LaravelHasManyMerged\HasManyMerged;
use Korridor\LaravelHasManyMerged\HasManyMergedRelation;

class User extends Model
{
    use HasManyMergedRelation;
    
    // ...

    /**
     * @return HasManyMerged<Message>
     */
    public function messages(): HasManyMerged
    {
        return $this->hasManyMerged(Message::class, ['sender_user_id', 'receiver_user_id']);
    }

    /**
     * @return HasMany<Message>
     */
    public function sentMessages(): HasMany
    {
        return $this->hasMany(Message::class, 'sender_user_id');
    }

    /**
     * @return HasMany<Message>
     */
    public function receivedMessages(): HasMany
    {
        return $this->hasMany(Message::class, 'receiver_user_id');
    }
}

Contributing

I am open for suggestions and contributions. Just create an issue or a pull request.

Local docker environment

The docker folder contains a local docker environment for development. The docker workspace has composer and xdebug installed.

docker-compose run workspace bash

Testing

The composer test command runs all tests with phpunit. The composer test-coverage command runs all tests with phpunit and creates a coverage report into the coverage folder.

Codeformatting/Linting

The composer fix command formats the code with php-cs-fixer. The composer lint command checks the code with phpcs.

License

This package is licensed under the MIT License (MIT). Please see license file for more information.

laravel-has-many-merged's People

Contributors

dependabot[bot] avatar korridor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

laravel-has-many-merged's Issues

withCount doesn't work

Everything's Great ๐Ÿ‘Œ
"with" method is working but only problem i know is that "withCount" method doesn't working

merging two relation BelongsToMany and hasMany and returning relation

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

I'm a bit confused!

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??

Order/Sort parent based on Childrens

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.

Trying to install with the "^0" for Laravel 8 but getting error

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!

Inverse relationship

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!

HasOne support?

It would be very nice to have HasOne support... nice work!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.