A little background. I am attempting to register a new user, create a default profile for them and then log them in. The users table as a current_profile_id
column.
I am using UUID's for this, there is no incrementing ID's as all. My migrations appear to be all set up correctly, but something is going wrong when the relationships are being saved. At the end of the process my newly created profile has no ID to it.
UuidTrait.php:
<?php
namespace MyNamespace\Traits;
use Ramsey\Uuid\Uuid;
use Illuminate\Database\Eloquent\ModelNotFoundException;
/**
* Trait UuidTrait
* @package MyNamspace\Traits
*/
trait UuidTrait
{
/**
* Boot function from laravel.
*/
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->{$model->getKeyName()} = Uuid::uuid4()->toString();
});
}
}
User.php (Showing the relationships only)
/**
* Has many profiles
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function profiles()
{
return $this->hasMany('MyNamespace\Models\Profile');
}
/**
* Current profile
*
* @return \Illuminate\Database\Eloquent\Relations\belongsTo
*/
public function currentProfile()
{
return $this->hasOne('MyNamespace\Models\Profile', 'id', 'current_profile_id');
}
AuthController.php (register() the creation of it all)
$user = $this->user->create($request->all());
$profile = $user->profiles()->create(['username' => $request->username]);
$user->currentProfile()->save($profile);
My user is being created with an ID but for some reason my profile is being created without. I have two suspicions. Either its my Trait as I am creating from a relationship or its the relationship, any ideas?