Daniel-Pablo's avatar

distribute money along all the users ? HOW ? on a MLM

Hi, I am creating an MLM multilevel market, am on my way of trying to pay all the people that are on the upper levels, but I want to ask first how would you manage it... so, there comes in 100 dollars, the 40% in this case 40 dollars most travel along with all the upper affiliates levels and distribute the money that way, so if there are 40 upper levels to each one The system most assign 1 dollar, but how to make this work... how to calculate all these tasks... because in the future there will be like 2.000.000.000 on each transaction waiting to be paid... how can this be managed without overhead the system or making a WHILE loop, I want the server make and event or make a list of task that he will be delivering the money to each account but when he got time to do it... got me?

I have the relationship for now of a subscribed user and his sponsor so I got like USER 2 is sponsor by USER 1 if user 2 affiliate someone the will be like USER 8 sponsor by USER 2 on a table record of sponsors, thanks in advance if someone can point me in the right direction.

0 likes
13 replies
bugsysha's avatar

@santino also feedback category is for topics which relate to issues on Laracasts. Like if video playback is not working or CSS is broken.

Daniel-Pablo's avatar

@bugsysha , thanks look, in a Multilevel business if you affiliate someone you will be the sponsor, each time your affiliate user buy something on the store online you will get a percentage of revenue but the person that affiliate you will get a revenue too and so on until get to the sponsor #1. Better now?

This is a job that must repeat each time someone buys something in the store, my concern is, how can I do it,

It will be like a loop so, is like

 this->user->getSponsor->money +=  1 //usd
//then
$user = this->user->sponsor
// this most repeat until I get the sponsor #1

This is better? do you now undestand me? a little drawing code here


		1           // this is the  user #1 and is the sponsor of 2 and 3
	2	     3   // here the user 1 affiliate the user 2 and 3
     4  5           6 7   // here the user 2 affiliate the user 4 and 5 

and so on i could have 1000 user affiliated, This is better? do you now undestand me?

Daniel-Pablo's avatar

@santino also feedback category is for topics which relate to issues on Laracasts. got it!! new time will post there on feeedback

bugsysha's avatar

The easiest thing you can do is have two foreign keys. If we take from what you've posted it will have

id, root_affiliate_id, parent_affiliate_id
1, 1, 1
2, 1, 1
3, 1, 1
4, 1, 2
5, 1, 2
6, 1, 3
7, 1, 3

That way at small cost you will be able to easily find everyone in the chain and still maintain parental records. Optimised for speed and usability.

Sorry for formatting, can't format as table for some reason.

Daniel-Pablo's avatar

@bugsysha, @jlrdw got this table

        Schema::create('sponsors', function (Blueprint $table) {

         $table->primary(['user_id', 'sponsor_id']);

         $table->unsignedBigInteger('user_id')->unsigned()->unique()->primaryKey();
         $table->bigInteger('sponsor_id');
         $table->string('sponsor_name');
         $table->string('pin_number');

         $table->timestamps();


     });

this table makes my relationship between the user and the person that is his sponsor,

now with this table in mind, if the user #432 buys a product I need for each his sponsor until the top, how can I manage it? that's the main question of all this thread, thanks a lot @bugsysha for your help on this matter, having in mind there can be millions of people with a lot of sponsors and buying at the same time...

bugsysha's avatar

From my example, you can use WHERE root_affiliate_id = x OR parent_affiliate_id = x. That should give you all records related to specific user so everything can be done in one query.

bugsysha's avatar
bugsysha
Best Answer
Level 61
Sponsor::where(function ($query) use ($id) {
	$query->where('root_affiliate_id', $id)
		->orWhere('parent_affiliate_id', $id);
})
	->each
	->increment($amount);

And you figure out amount based on what is the total and divide it by the number of sponsors that match that query.

Please or to participate in this conversation.