title: Laravel app is trying to insert a value in wrong column
body: <p>I have a application that I am trying to add a second Module to.   </p>
  
  <p>Basically i'm cloning an existing module that is already working with my database. All code is exactly the same as the original/working module, other than a different table name and different controller/class/model names. </p>
  
  <p>I have 3 new tables that are related to the new module, just like with the old module <code>investments</code>, <code>investment_categories</code> and <code>investment_vendors</code>.<br>
  Every time I try to use my new module it wants to upload the vendor and category name value to the <code>investments</code> table when it should be uploading that to the respective <code>investment_categories</code> and <code>investment_vendor</code> tables. </p>
  
  <p>I don't believe the issue is with my new module code, but somewhere else within composer.<br>
  I don't expect that I would need to run <code>composer dump-autoload</code> as I've always edited my modules without running that (but this is my first time updating one to query/write to a different table).   </p>
  
  <p>Is there something else I need to be running to get this to work?</p>
  
  <p>The below error I see in the log is:  </p>
  
  <blockquote>
    <p>[2018-01-24 21:13:21] production.ERROR: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category_name' in 'field list' in /var/www//vendor/laravel/framework/src/Illuminate/Database/Connection.php:390</p>
  </blockquote>
  
  <p><strong>Controller File:</strong></p>
  
  <pre><code>&lt;?php
  
  namespace MC\Modules\Investments\Controllers;
  
  use MC\Http\Controllers\Controller;
  use MC\Modules\Investments\Models\Investment;
  use MC\Modules\Investments\Models\InvestmentCategory;
  use MC\Modules\Investments\Models\InvestmentVendor;
  
  class InvestmentController extends Controller
  {
      use ReturnUrl;
  
      public function index()
      {
          $this-&gt;setReturnUrl();
  
          $investments = Investment::defaultQuery()
              -&gt;keywords(request('search'))
              -&gt;categoryId(request('category'))
              -&gt;vendorId(request('vendor'))
              -&gt;sortable(['investment_date' =&gt; 'desc'])
              -&gt;paginate(config('mc.defaultNumPerPage'));
  
          return view('investments.index')
              -&gt;with('investments', $investments)
              -&gt;with('displaySearch', true)
              -&gt;with('categories', ['' =&gt; trans('mc.all_categories')] + InvestmentCategory::getList())
              -&gt;with('vendors', ['' =&gt; trans('mc.all_vendors')] + InvestmentVendor::getList())
      }
  </code></pre>
  
  <p><strong>Create Controller File:</strong></p>
  
  <pre><code>&lt;?php
  
  namespace MC\Modules\Investments\Controllers;
  
  use MC\Http\Controllers\Controller;
  use MC\Modules\Investments\Models\Investment;
  
  class InvestmentCreateController extends Controller
  {
      public function create()
      {
          return view('investments.form')
              -&gt;with('editMode', false)
              -&gt;with('currentDate', DateFormatter::format(date('Y-m-d')))
      }
  
      public function store()
      {
          $record = request()-&gt;except('attachments');
  
          $record['investment_date'] = DateFormatter::unformat($record['investment_date']);
          $record['amount'] = NumberFormatter::unformat($record['amount']);
          $record['tax'] = ($record['tax']) ? NumberFormatter::unformat($record['tax']) : 0;
  
          $investment = Investment::create($record);
  
          return redirect($this-&gt;getReturnUrl())
              -&gt;with('alertSuccess', trans('mc.record_successfully_created'));
      }
  }
  </code></pre>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>&lt;?php
  
  namespace MC\Modules\Investments\Models;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Investment extends Model
  {
      use Sortable;
  
      protected $table = 'investments';
  
      protected $guarded = ['id'];
  
      protected $sortable = ['investment_date', 'investment_categories.name', 'description', 'amount'];
  
      public static function boot()
      {
          parent::boot();
      }
  
      /*
      |--------------------------------------------------------------------------
      | Relationships
      |--------------------------------------------------------------------------
      */
  
      public function attachments()
      {
          return $this-&gt;morphMany('MC\Modules\Attachments\Models\Attachment', 'attachable');
      }
  
      public function category()
      {
          return $this-&gt;belongsTo('MC\Modules\Investments\Models\InvestmentCategory');
      }
  
      public function vendor()
      {
          return $this-&gt;belongsTo('MC\Modules\Investments\Models\InvestmentVendor');
      }
  
      /*
      |--------------------------------------------------------------------------
      | Accessors
      |--------------------------------------------------------------------------
      */
  
      public function getAttachmentPathAttribute()
      {
          return attachment_path('investments/' . $this-&gt;id);
      }
  
      public function getFormattedAmountAttribute()
      {
          return CurrencyFormatter::format($this-&gt;amount);
      }
  
      public function getFormattedTaxAttribute()
      {
          return CurrencyFormatter::format($this-&gt;tax);
      }
  
      public function getFormattedInvestmentDateAttribute()
      {
          return DateFormatter::format($this-&gt;investment_date);
      }
  
  
      /*
      |--------------------------------------------------------------------------
      | Scopes
      |--------------------------------------------------------------------------
      */
  
      public function scopeCategoryId($query, $categoryId = null)
      {
          if ($categoryId)
          {
              $query-&gt;where('category_id', $categoryId);
          }
  
          return $query;
      }
  
      public function scopeDefaultQuery($query)
      {
          return $query-&gt;select('investments.*', 'investment_categories.name AS category_name',
              'investment_vendors.name AS vendor_name', 'clients.unique_name AS client_name')
              -&gt;join('investment_categories', 'investment_categories.id', '=', 'investments.category_id')
              -&gt;leftJoin('investment_vendors', 'investment_vendors.id', '=', 'investments.vendor_id');
      }
  
      public function scopeVendorId($query, $vendorId = null)
      {
          if ($vendorId)
          {
              $query-&gt;where('vendor_id', $vendorId);
          }
  
          return $query;
      }
  }
  </code></pre>
  
  <p>One thing to note is that I do have existing data in the table and the query works fine when viewing it from my web page, however the issue is when trying to edit it or insert new data/record.</p>
  
  <p>As you can see from the model above, </p>
  
  <blockquote>
    <p>investments.*', 'investment_categories.name AS category_name</p>
  </blockquote>
  
  <p>the query works because of that command.<br>
  I cannot find anywhere else where it determines how to take <code>"investment_categories.name as category_name"</code>.<br>
  Can you tell me why it is trying to write the <code>category_name</code> value to the <code>investments</code> table instead of the <code>investment_categories</code> table's <code>name</code> column?</p>