Multi foreach sebanyak jumlah level pada aplikasi MLM

Salam,

Saya sedang membangun aplikasi multi level marketing dengan Matrix 3x12 (Tiga kaki sampai kedalaman level 12). Dalam menampilkan data jaringan pada masing2 level saya melakukan foreach sebanyak level tersebut dan itu berjalan sesuai harapan tapi saya ragu bahwa cara yang saya gunakan ini efektif atau tidak ketika jumlah member sudah banyak.

Framework Laravel 5.4

Tabel : Promotor dan Member

Model relasi : Promotor->hasMany->Member

Dibawah ini query yang saya gunakan untuk menampilkan data pada level 12, Pada level 1 sampai 11 saya juga menggunakan cara yang sama, dengan jumlah foreach sebanyak level tsb.

public function level12()
    {
        $output = '';
        $user = Auth::user()->id;
        $jaringan = Promotor::with('members')->where('user_id',$user)->first();

            foreach($jaringan->members as $network)
            {
                $promotorid = $network->user_id;
                $level2 = Promotor::with('members')->where('user_id',$promotorid)->first();
                foreach($level2->members as $network2)
                {
                    $promotorid2 = $network2->user_id;
                    $level3 = Promotor::with('members')->where('user_id',$promotorid2)->first();
                    foreach($level3->members as $network3)
                    {
                        $promotorid3 = $network3->user_id;
                        $level4 = Promotor::with('members')->where('user_id',$promotorid3)->first();
                        foreach($level4->members as $network4)
                        {
                            $promotorid4 = $network4->user_id;
                            $level5 = Promotor::with('members')->where('user_id',$promotorid4)->first();
                            foreach($level5->members as $network5)
                            {
                                $promotorid5 = $network5->user_id;
                                $level6 = Promotor::with('members')->where('user_id',$promotorid5)->first();
                                foreach($level6->members as $network6)
                                {
                                    $promotorid6 = $network6->user_id;
                                    $level7 = Promotor::with('members')->where('user_id',$promotorid6)->first();
                                    foreach($level7->members as $network7)
                                    {
                                        $promotorid7 = $network7->user_id;
                                        $level8 = Promotor::with('members')->where('user_id',$promotorid7)->first();
                                        foreach($level8->members as $network8)
                                        {
                                            $promotorid8 = $network8->user_id;
                                            $level9 = Promotor::with('members')->where('user_id',$promotorid8)->first();
                                            foreach($level9->members as $network9)
                                            {
                                                $promotorid9 = $network9->user_id;
                                                $level10 = Promotor::with('members')->where('user_id',$promotorid9)->first();
                                                foreach($level10->members as $network10)
                                                {
                                                    $promotorid10 = $network10->user_id;
                                                    $level11 = Promotor::with('members')->where('user_id',$promotorid10)->first();
                                                    foreach($level11->members as $network11)
                                                    {
                                                        $promotorid11 = $network11->user_id;
                                                        $level12 = Promotor::with('members')->where('user_id',$promotorid11)->first();
                                                        foreach($level12->members as $network12)
                                                        {
                                                            $promotorid12 = $network12->user_id;
                                                            $query12 = Promotor::with('members')->where('user_id',$promotorid12)->first();
                                                            $output .= '<a href="#" class="list-group-item datalevel12" id="datalevel12">'.$network12->name.'
                                                                <span class="badge"><span class="glyphicon glyphicon-arrow-down"></span> : '.$query12->members->count().'</span>
                                                                <span class="badge"><span class="glyphicon glyphicon-arrow-up"></span> : '.$network12->promotor->name.'</span>
                                                                </a>';
                                                        }

                                                    }

                                                }

                                            }

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

            }
            $output .= '<a href="#" class="list-group-item" id="datalevel12">Member : <span id="totaldatalevel12"></span> | Target : 531.441</a>';
        echo $output;

    }
avatar Supriono
@Supriono

84 Kontribusi 16 Poin

Dipost 3 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban