jeudi 24 juin 2021

I need to update mysql table when someone use my referral code

I need to update mysql table when someone use my referral code which is store in my route using cookie.

i want to check if the cookie ref is available , if it is available two table Wallet and Referral will be updated with the transaction id and the balance on the wallet. I keep getting a success message which means the transaction is working but the table is not update. Below is my code. Thanks

Route


Route::redirect('/', '/index', 301);
Route::get('/index', function() {
    if(!empty($_GET['ref'])) {
        $ref = $_GET['ref'];
        setcookie('ref', $ref, time() + (86400), "/");
        // return redirect()->route('power.index', ['ref' => $ref]);
        // print_r($_COOKIE['ref']);
       return redirect()->route('power.index');
    } else {
        return view('index');
    }
    
})->name('index');

Referral Table

namespace App;

use Illuminate\Database\Eloquent\Model;

class Referral extends Model
{
    //
    protected $fillable = [
        'reftrans', 
        'refuserid', 
        'refamount', 
        'reftype'];
        
        protected $table = 'referrals';
        
        
}


UserTable

namespace App;

use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'fname', 'lname', 'email', 'password', 'phone', 'gLocatorID', 'role_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $with = [
        'wallet', 'vendor'
    ];

    // Rest omitted for brevity

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    public function wallet()
    {
        return $this->hasOne(Wallet::class);
    }

    public function vendor() {
        return $this->hasOne(Vendor::class);
    }

    // public function getPhoneAttribute() {
    //     return "+234" . $this->phone;
    // }
}

Wallet Table

namespace App;

use Illuminate\Database\Eloquent\Model;

class Wallet extends Model
{
    protected $fillable = [
        'user_id', 'balance'
    ];

    public function owner()
    {
        return $this->belongsTo(User::class);
    }

    public function transactions()
    {
        return $this->hasMany(WalletTransaction::class);
    }
}


Controller


class PowerController extends Controller
{

                 if(\Auth::check()) {
                 $rUserID = \Auth::id();
                     dd(cookie::get['ref']);
                     dd($ref);
                     if (isset($ref)){
                         
                     if ($ref == $rUserID){
                     
                     //define amount n type
                     $amt = 25.00;
                     $type = 'Power';
                     //Updating Wallet
                     $wallet = \App\Wallet::find($user_id)->rUserID;
                     $wallet->balance = $wallet->balance + $amt;  
                     $wallet->save(); 
                     
                     //Updating Referral
                     $referral = new \App\Referral;    
                     $referral->reftrans = $powerTransaction->id; 
                     $referral->refuserid = $rUserID; 
                     $referral->refamount = $amt; 
                     $referral->reftype = $type; 
                     $referral->save(); 
                     }
                 }else {
                 //do nothing
             }else{
                 //do nothing
            }
                   
}
 ``` 


Referral link

<input type="text" class="form-control" value="" id="myInput">


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3gUZVLy
via IFTTT

Aucun commentaire:

Enregistrer un commentaire