Laravel - Give upline (indirect) referral bonus - laravel

I am working on a referral system, when a user shares his referral link to a guest, and the guest registers the user will be given a bonus (working as expected), but now what I am trying to do is
I would like to give a bonus to the first user that referred a user
For example:
A referred B and
B referred C
Anytime B referred a new user give A bonus
So if C referred a new user give B bonus
Below is my code:
$referrer = User::whereUsername(session()->pull('referrer'))->first();
$user = User::create([
'username' => $request->username,
'name' => $request->name,
'referrer_id' => $referrer ? $referrer->id : null,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
if ($user->referrer !== null)
{
$user->referrer->deposit(400);
// Notification::send($user->referrer, new ReferrerBonus($user));
}
On model:
/**
* A user has a referrer.
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function referrer()
{
return $this->belongsTo(User::class, 'referrer_id', 'id');
}
/**
* A user has many referrals.
*
* #return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function referrals()
{
return $this->hasMany(User::class, 'referrer_id', 'id');
}
On migration file:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username')->unique();
$table->unsignedBigInteger('referrer_id')->nullable();
$table->foreign('referrer_id')->references('id')->on('users');
$table->string('email')->unique();
$table->integer('is_admin')->default(0);
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});

Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username')->unique();
$table->unsignedBigInteger('referrer_id')->nullable();
$table->foreign('referrer_id')->references('id')->on('users');
$table->string('email')->unique();
$table->integer('is_admin')->default(0);
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});

Related

General error: 1364 Field 'name' doesn't have a default value - Laravel 9.x

I am inserting data into my db using the following fields but when I try to insert, I keep getting this error General error: 1364 Field 'name' doesn't have a default value. I am using Laravel 9.x
What could I be doing wrong?
I have tried the following approaches but all not working.
Initially, This is how this migration looks like
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
I saw this similar question Similar Question, and I followed some of the answers given by doing this but nothing worked
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('username')->unique()->nullable();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
And I tried making them default(''); still, nothing worked.
When I try using the nullable(); this is what I get in the database
This is my function
public function rules()
{
return [
'name' => 'required|string|min:3|max:255',
'username' => 'required|string|unique:users,username|min:3|max:255',
'email' => 'required|string|email|unique:users,email',
'password' => 'required|string|min:8|max:255'
];
}
This is my protected items
protected $fillable = [
'name',
'username',
'email',
'password',
];
This is another similar question Similar Question but does not help either
I have other similar questions but most of them are old and does not answer my question too

Laravel save HasMany realtion

I have a set that has a list of group and each group has a list of attributes like this
//sets
Schema::create('sets', function (Blueprint $table) {
$table->id();
$table->string('name');
});
//groups
Schema::create('groups', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->foreignId('set_id')->constrained('sets')->onDelete('cascade');
});
//attributes
Schema::create('attributes', function (Blueprint $table) {
$table->id();
$table->string('name');
});
//set_group_attributes
Schema::create('set_group_attributes', function (Blueprint $table) {
$table->foreignId('set_id')->constrained('sets')->onDelete('cascade');
$table->foreignId('group_id')->constrained('groups')->onDelete('cascade');
$table->foreignId('attribute_id')->constrained('attributes')->onDelete('cascade');
$table->integer('position');
$table->unique(['set_id', 'attribute_id']);
});
I need to save the set with groups and attributes, this is my code :
$set = Set::create(['name' => $request->name]); ---> save the set
$set->groups()->createMany($request->groups); ---> save the group
//set model
public function groups(): HasMany
{
return $this->hasMany(Group::class, 'set_id','id');
}
My question is how to save the attributes with laravel eloquent?
Here we can use attach like in Docs with belongsToMany
//group model
/**
* #return BelongsToMany
*/
public function attributes(): BelongsToMany
{
return $this->belongsToMany(Attribute::class, 'set_group_attributes', 'group_id', 'attribute_id')->withPivot('position');
}
// save attributes of groups
foreach ($attributes as $attribute) {
$group->attributes()->attach($attribute['id'], ['set_id' => $model->id, 'position' => $attribute['position']]);
}

Does not change default value in mysql (laravel 5,5)

this is my code in users tabel (migrations)
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('level')->default('user');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
when I want to change level manually like below code
return User::create([
'level' => 'admin',
'name' => 'emad',
'email' => 'emad#gmail.com',
'password' => bcrypt('123456')
]);
In the level field, the same user value is saved and admin dosent save.
I do not know why?
Judging from the little code here, i would say you haven't added the level column to the fillable model variable.Like so:
protected $fillable = ['level','any_other_field_you_want_to_assign'];
Add this line of code to the top of your User model class
Check the documentation here

Storing foreign key value in database (laravel)

I am new to laravel. i am having difficulties to store foreign key value in database. I have read the documentation, searched on stackoverflow. But its confusing me more.
I have a User table and an idea table. User can create many idea. Let me show the code:
Controller function:
public function storePost(IdeaRequest $request) {
Idea::create([
'title' => $request->input('idea_title'),
'user_id' => $request->hasMany('user_id'),
'image' => $request->file('idea_image')->move('images'),
'info' => $request->input('idea_info'),
'communitySelection' => $request->input('selection'),
'location' => $request->input('idea_location'),
'goal' => $request->input('idea_goal'),
'description' => $request->input('idea_description')
]);
}
Idea Model:
class Idea extends Model
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'idea_title', 'idea_info','user_id', 'idea_image', 'idea_description', 'duration', 'idea_goal', 'pledge_amount', 'set_equity',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'remember_token',
];
public function Post() {
return $this->hasMany('User');
}
}
Idea Migration:
public function up()
{
//
Schema::create('ideas', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('idea_title');
$table->string('idea_info', 150);
$table->string('idea_image');
$table->string('selection');
$table->longText('idea_description');
$table->string('idea_location');
$table->integer('idea_goal')->unsigned();
$table->integer('pledge_amount')->unsigned();
$table->string('set_equity')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
User Migration:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name',50);
$table->string('email')->unique();
$table->string('password',32);
$table->string('profile_picture')->nullable();
$table->string('address');
$table->string('phone_number',15);
$table->boolean('status')->nullable();
$table->string('user_type',15);
$table->string('credit_card',16)->unique();
$table->rememberToken();
$table->timestamps();
});
}
It can be seen that foreign key is being set. I am unable to store the value of foreign key on a user table. Any help would be appreciated.
In the Idea Migration, you can declare the foreign key like this
public function up()
{
//
Schema::create('ideas', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('idea_title');
$table->string('idea_info', 150);
$table->string('idea_image');
$table->string('selection');
$table->longText('idea_description');
$table->string('idea_location');
$table->integer('idea_goal')->unsigned();
$table->integer('pledge_amount')->unsigned();
$table->string('set_equity')->nullable();
$table->rememberToken();
$table->timestamps();
});
//Declare foreign key
Schema::table('ideas', function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')
->onDelete('restrict')
->onUpdate('restrict');
});
}
I do not agree with the relation you added in the Idea Model. You should use belongsTo instead of hasMany. And according to the comments, change Post() to User() .
public function User() {
return $this->belongsTo('User');
}
If so, you need also change 'user_id' => $request->hasMany('user_id') in the Controller function.

Laravel 5 How to add multiple foreign key constraints to a User on creation?

I now know that I can create a User using the haveMany relationship between a Region and User by doing this:
$region = Region::find($data['region_id']);
$region->users()->create([
'username' => $data['username'],
'email' => $data['email'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'password' => bcrypt($data['password']),
]);
But what if there are multiple foreign keys, how do you create a user without making them fillable for more than 1 foreign key constraint? What if there were 3 or 4?
Example of the relationships setup in the different models:
Region hasMany Users, or
User belongsTo a Region
Location hasMany Users, or
User belongsTo a Location
A stripped down version of my migration looks like this if it helps to understand the relationships better:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('region_id')->unsigned();
$table->integer('location_id')->unsigned();
$table->timestamps();
$table->foreign('region_id')
->references('id')
->on('regions');
$table->foreign('location_id')
->references('id')
->on('locations');
});
Schema::create('regions', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 100);
$table->timestamps();
});
Schema::create('locations', function (Blueprint $table) {
$table->increments('id');
$table->string('street_address', 100);
$table->string('city', 50);
$table->string('province', 50);
$table->string('country', 50);
$table->string('postal_code', 10);
$table->timestamps();
});
Example of Region and Location Models:
/**
* Region may have many users.
*
* #return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function users()
{
return $this->hasMany('App\User');
}
/**
* Location may have many users.
*
* #return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function users()
{
return $this->hasMany('App\Location');
}
I assume you have a model for Region and Location. Each of your region has an id and location has an id, then just add
$this->hasMany('where your Region model is', 'region_id', 'id');
$this->hasMany('where your Location model is', 'location_id', 'id');
to your User model
Reference: http://laravel.com/docs/5.0/eloquent#one-to-many
Edit: You can also do in any of your model
if ($this->create()) {
return (new Model())->create(the params you need in another model);
}

Resources