Last active
October 24, 2019 06:08
-
-
Save bayareawebpro/14e82f66691766861603908c42417e92 to your computer and use it in GitHub Desktop.
Filling a Model Relationship as an attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* HasOne Affiliate Relationship | |
* @return \Illuminate\Database\Eloquent\Relations\hasOne | |
*/ | |
function affiliate(){ | |
return $this->hasOne(Affiliate::class, 'user_id','id'); | |
} | |
/** | |
* Fill Affiliate Relationship | |
* @return void | |
*/ | |
function setAffiliateAttribute(array $values){ | |
if($this->affiliate()->exists()){ | |
$this->affiliate()->first()->update($values); | |
}else{ | |
$this->affiliate()->save(new Affiliate($value)); | |
} | |
} | |
//Controller: | |
$user = $request->user(); | |
$user->fill([ | |
'affiliate' => $request->all() | |
]); | |
$user->save(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment