kjdion84 kjdion84 - 3 years ago 92
MySQL Question

Eloquent using wrong table name

I have a model called

ReferrerMedium
along with a migration for a
referrer_mediums
table.

Here is my class:

namespace App;

class ReferrerMedium extends \Eloquent
{
//
}


Here is my migration:

Schema::create('referrer_mediums', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});


Here is my code:

foreach (ReferrerMedium::all() as $referrer_medium) {
$options[$referrer_medium->name] = $referrer_medium->name;
}


This code is causing the error
Base table or view not found: 1146 Table 'leadbind.referrer_media' doesn't exist


Why is it attempting to query the
referrer_media
table instead of
referrer_mediums
???

Answer Source

Because medium's plural type is media, therefore you should manually specified table name in you model:

$protected $table = 'referrer_mediums';

But i recommend that you should make a migration to change the table name.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download