亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 6 - 聯盟跟蹤用戶注冊

Laravel 6 - 聯盟跟蹤用戶注冊

PHP
波斯汪 2022-10-14 14:58:23
正如你們所看到的Image Above,我想創建用戶可以注冊和輸入referral_user的推薦系統affiliate users。并且Referral_Code對每個用戶都是獨一無二的。My Problem is I cant track Whom that code is.My User Schema       Schema::create('users', function (Blueprint $table) {            $table->bigIncrements('id');            $table->unsignedBigInteger('referrer_id')->nullable();            $table->foreign('referrer_id')->references('id')->on('users');            $table->string('referral_code')->unique()->nullable();            $table->string('referred_by')->nullable();            $table->string('name');            $table->string('email')->unique();            $table->string('password');            $table->rememberToken();            $table->timestamps();        });My User Model    public function referrer()    {        return $this->belongsTo(User::class);    }    public function referrals()    {        return $this->hasMany(User::class);    }In My UserController       $referrer = User::where('name', auth()->user()->name)->first();        $user = new User();        $user->name = $request->name;        $user->referral_code = substr(uniqid(), 0, 8); // for unique id        $user->email = $request->email;        $user->referrer_id = $referrer ? $referrer->id : null;        $user->role = $request->role;        $user->password = bcrypt($request->password);        $user->save();        return response()->json([            'created' => true,        ]);希望你們能給我一些想法,如果你們發現我的代碼有錯誤,請糾正我,我會感謝你們的幫助。謝謝...
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

我相信你混淆了 Laravel 的關系。hasOne, belongsTo, etc不是為在同一模型中使用而設計的?;旧夏阍赨sers模型中所說的是“用戶有一個用戶,其中 user_id = id”這是沒有意義的。


我不知道您是如何設計 sql 表的,我建議您將實體拆分為不同的部分,并使用數據透視表來綁定它們:users, affiliates, referrals


- users table have     : "id"

- affiliates table have: "id", "refferal_code"

- refferals table have : "id", "affiliate_id", "user_id", "refferal_code"


refferals您通過表,通過他們的 ID將用戶與會員聯系起來。


然后在它們之間建立關系。您可能還想閱讀更多關于Eloquent 關系的信息。希望這個答案對您有所幫助。


查看完整回答
反對 回復 2022-10-14
  • 1 回答
  • 0 關注
  • 109 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號