Jan
Jan

Reputation: 572

Laravel eloquent cannot determine table

I have created a database seeder to seed my table which I have created via a migration just a few seconds ago. However, I get this error when running the seeder:

In Connection.php line 664:

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'trier_taxibus.ca
  tegories' doesn't exist (SQL: insert into `categories` (`category`, `update
  d_at`, `created_at`) values (ISF, 2018-07-10 14:16:08, 2018-07-10 14:16:08)
  )


In Connection.php line 452:

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'trier_taxibus.ca
  tegories' doesn't exist

The SQL Statement looks fine to me and is working as well if I copy and paste it to PhpMyAdmin. I have also created a model and defined the table name as well as the fillable columns..:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Category extends Model
{
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'categories';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'category'
    ];

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];
}

This is my Database Seeder:

use Illuminate\Database\Seeder;
use App\Category;

class CategoryTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $category = new Category();
        $category->category = 'ISF';
        $category->save();
    }
}

And this is what I get returned if I select the table categories in my tool Sequel Pro:

CREATE TABLE `categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `category` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Seems all fine to me. However, I get this error above. Any ideas on that?

Kind regards

Upvotes: 1

Views: 688

Answers (3)

Behzad kahvand
Behzad kahvand

Reputation: 446

you should check your db connection in your .env file or you can try this in your model :

protected $connection = 'trier_taxibus'; 

Upvotes: 1

Shreeraj
Shreeraj

Reputation: 767

It seems like you are getting error because you have kept $table as "protected" in your model. Change it to "public".

Like this:

public $table = "categories";
protected $primaryKey = "id";

Also check your .env file if you have added correct database, username and password.

Upvotes: 0

mitesh
mitesh

Reputation: 143

you can use below code

public function run()
    {
        $row = array('category' => 'Purchasing',
                    'created_at'    => Carbon::now(),
                    'updated_at'    => Carbon::now());
        DB::table('categories')->insert($row);
    }

intead of eloquent

Upvotes: 1

Related Questions