2022-06-02 14:11:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use App\Models\User;
|
2023-01-29 22:12:34 +00:00
|
|
|
use App\Traits\Referral;
|
2022-06-02 14:11:24 +00:00
|
|
|
use Illuminate\Database\Migrations\Migration;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
2023-01-05 17:03:31 +00:00
|
|
|
return new class extends Migration
|
2022-06-02 14:11:24 +00:00
|
|
|
{
|
2023-01-29 22:12:34 +00:00
|
|
|
use Referral;
|
|
|
|
public function setReferralCode($userid)
|
2023-01-05 17:01:42 +00:00
|
|
|
{
|
2023-01-29 22:12:34 +00:00
|
|
|
$code = $this->createReferralCode();
|
|
|
|
DB::table('users')
|
|
|
|
->where('id', '=', $userid)
|
|
|
|
->update(['referral_code' => $code]);
|
2022-06-07 08:18:33 +00:00
|
|
|
}
|
2023-01-05 17:01:42 +00:00
|
|
|
|
2022-06-02 14:11:24 +00:00
|
|
|
/**
|
|
|
|
* Run the migrations.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function up()
|
|
|
|
{
|
|
|
|
Schema::table('users', function (Blueprint $table) {
|
|
|
|
$table->string('referral_code')->lenght(8)->nullable();
|
|
|
|
});
|
|
|
|
|
2023-01-05 17:01:42 +00:00
|
|
|
$existing_user = User::where('referral_code', '')->orWhere('referral_code', null)->get();
|
2022-06-02 14:11:24 +00:00
|
|
|
|
|
|
|
foreach ($existing_user as $user) {
|
2023-01-29 22:12:34 +00:00
|
|
|
$this->setReferralCode($user->id);
|
2022-06-02 14:11:24 +00:00
|
|
|
}
|
2023-01-05 17:01:42 +00:00
|
|
|
}
|
2022-06-02 14:11:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse the migrations.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function down()
|
|
|
|
{
|
|
|
|
Schema::table('users', function (Blueprint $table) {
|
|
|
|
$table->dropColumn('referral_code');
|
|
|
|
});
|
|
|
|
}
|
2023-01-05 17:03:31 +00:00
|
|
|
};
|