Is there a better way to assign an Eloquent relationship in Laravel using a Repository?
<p>Assuming I have these 2 Models <code>User</code> and <code>Account</code>.
<br>
Where a <code>User</code> can have multiple <code>Accounts</code>. (hasMany)
<br>
And an <code>Account</code> belongs to a <code>User</code>. (belongsTo)</p>

<p>If I'm <strong>not</strong> using a Repository I would save the model and its relationship like this:</p>

<pre><code>    $account = new Account();
    $account-&gt;username = $this-&gt;username;
    $account-&gt;secret = $this-&gt;secret;
    $account-&gt;user()-&gt;associate($this-&gt;user);
    $account-&gt;save();
</code></pre>

<p>And when using a Repository I see many people (and articles) doing it like this:</p>

<pre><code>$accountRepository-&gt;create([
      'username' =&gt; $this-&gt;username,
      'secret'   =&gt; $this-&gt;secret,
      'user_id'  =&gt; $this-&gt;user()-&gt;id,
]);
</code></pre>

<p>My problem with this approach is in the <code>user_id</code> because it doesn't feel safe to manually assign this kind of relationship, additionally when someone else is reading this code he cannot tell what is the relation between the <code>Account</code> and <code>User</code> from there!!</p>

<p>The way I'm currently handling this is as follow: <em>(it's kinda combination of the both ways)</em></p>

<pre><code>    // create the account and attach the fields and the relationships
    $account = new Account();
    $account-&gt;username = $this-&gt;username;
    $account-&gt;secret = $this-&gt;secret;
    $account-&gt;user()-&gt;associate($this-&gt;user);

    // save the new created model with the repository
    $account = $accountRepository-&gt;create($account-&gt;toArray());
</code></pre>

<p>But I'm not really sure if there's any better way to do this!!
All I need is to use the <code>associate</code> function because it feels safer and looks better for the readers. And to use a Repository to save and access my data.</p>

<p>Note: I am using the <a href="https://github.com/andersao/l5-repository" rel="noreferrer">https://github.com/andersao/l5-repository</a> to abstract the database layer.</p>