Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jwavess's avatar

What step am I missing?

I am trying to follow through with Eloquent 101.

shh into vagrant cd Code/projectone php artisan make:migration food --create="food"

(open food file) In the good file add the tables

$table->text('vegetables'); $table->text('fruit'); $table->text('grains');

php artisan make:model food php artisan tinker $food = new App\food; $food->vegetables ="eggplant"; $food->fruit="orange"; $food->grains="bread"; $food->save();

Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.testones' doesn't exist (SQL: insert into testones (vegetables, fruit, grains, updated_at, created_at) values (eggplant, orange, bread, 2016-01-08 23:03:01, 2016-01-08 23:03:01))'

What did I miss, in exactly that order from scratch?

0 likes
11 replies
jwavess's avatar

Where in the steps above do I run it, to make sure I am running it at the correct time.

zachleigh's avatar

After you create the migration and enter your table data.
So, create your migration:

php artisan make:migration create_food_table --create=food

Fill it out:

$table->text('vegetables');
$table->text('fruit');
$table->text('grains');

Run the migration:

php artisan migrate
jwavess's avatar

I am trying to follow through with Eloquent 101. shh into vagrant.

cd Code/projectone; php artisan make:migration create_food_table --create=food

(open food file) In the good file add the rows

$table->timestampe('published_at');
$table->text('vegetables');
$table->text('fruit');
$table->text('grains');

php artisan migrate

php artisan make:model food
php artisan tinker
$food = new App\food;
$food->vegetables = "eggplant";
$food->fruit = "orange";
$food->grains = "bread";
$food->published_at = Carbon\Carbon::now();
$food->save();

jwavess's avatar

so weird, so I did the steps above this answer and I still got the error below AND I checked the database and the food table is in the homestead databases. Im using Mysql, idk if that makes a differences @zachleigh

Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.foods' doesn't exist (SQL: insert into foods (vegetables, fruit, grains, published_at, updated_at, created_at) values (eggplant, orange, bread, 2016-01-09 02:03:32, 2016-01-09 02:03:38, 2016-01-09 02:03:38))'

zachleigh's avatar
Level 47

You created a table called 'food' but Laravel is looking for 'foods.' The name has to be exact. By convention, Laravel will assume that a model name is the singular of the database name. For example, if the model is called User, the database table would be called users. In your case, if the model is called Food, Laravel will look for a table called foods. To override this, set the table property in your model.

protected $table = 'food';
2 likes
jwavess's avatar

@zachleigh okay so I got it to work. Only thing is when I tried to add the "published_at" I received an error stating it didn't exist. When it dose, after all its just a column name.

jwavess's avatar

@zachleigh not to bring the topic back up but i got everything to work with just adding an 's' at the end of the table name. So what do you mean by " To override this, set the table property in your model. protected $table = 'food';"

Could you show me exactly how it should look and exactly what files to change it to or perhaps just edit my path above and paste a new version of it including the altercation of allowing food instead of foods.

Seth

skliche's avatar

@jwavess Looking at your commands you should have a model file stored at app/Food.php. Add it right here:

class Food extends Model {
    protected $table = 'food';

But do yourself a favor and stick with the names that Eloquent expects, i.e. table name foods instead of food.

Btw, please have a look at the markdown tags help that is linked right below the box where you enter your comments. The stuff you've posted so far in this thread is not very easy to read.

1 like
jwavess's avatar

@skliche gotcha, thank you! And I will keep that in mind for the next question.

Please or to participate in this conversation.