I want to update my record if it exists but create new one if not exists. here is i got so far:
MerchantBranch.php
public function token()
{
return $this->hasOne('App\MerchantBranchToken');
}
MerchantBranchToken.php
public function merchant_branch()
{
return $this->belongsTo('App\MerchantBranch');
}
$find = MerchantBranchToken::find($id);
if (!$find) {
$branch = new MerchantBranchToken(['token' => $token]);
MerchantBranch::find($id)->token()->save($branch);
} else {
$find->token = $token;
$find->save();
}
It's working perfectly.
But as i know Laravel is very powerful for its eloquent model. Can I make it shorter? or i already doing it correctly?.
I've tried using "updateOrCreate" method but my foreign key "merchant_branch_id" need to be fillable.