Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayareawebpro/14e82f66691766861603908c42417e92 to your computer and use it in GitHub Desktop.
Save bayareawebpro/14e82f66691766861603908c42417e92 to your computer and use it in GitHub Desktop.
Filling a Model Relationship as an attribute
<?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