title: How to insert a record using Eloquent (Laravel) relationships?
body: <p>I have two tables: <code>UserActivities</code> and <code>Activities</code>.</p>
  
  <p>Activities:</p>
  
  <pre><code>| Column | Type             |
  |--------|------------------|
  | id     | int              |
  | name   | varchar(255)     |
  |        |                  |
  </code></pre>
  
  <p>UserActivities:</p>
  
  <pre><code>| Column      | Type | Comment                  |
  |-------------|------|--------------------------|
  | id          | int  |                          |
  | user_id     | int  |                          |
  | activity_id | int  | //id of activity by name |
  </code></pre>
  
  <p>Now to insert an item Item into <code>UserActivity</code> Model I have to write two statements like this</p>
  
  <pre><code>$activity = Activity::where('name', '=', 'user_login')-&gt;first();
  UserActivity::create(['user_id' =&gt; Auth::login(), 'activity_id' =&gt; $activity-&gt;id]);
  </code></pre>
  
  <p>While this is fine, but I was just reading up on <a href="https://laravel.com/docs/5.6/eloquent-relationships#has-many-through" rel="nofollow noreferrer">Eloquent relationships</a> and wondering if there was a way to create <code>belongsTo</code> or some relationship and insert it directly like this</p>
  
  <pre><code>UserActivity::create(['user_id' =&gt; Auth::login(), 'activity' =&gt; 'user_login']);
  </code></pre>
  
  <p>And it automatically inserts <code>activity_id</code> by searching the name? Is it possible?</p>