php - I am trying to insert some data into status table but getting SQL Query Error in Laravel

I am new in Laravel. I have a table, and that table name is {status}.

enter image description here

Under the Models\ Status.php when I removed protected $table='status'; from status.php then I am getting this error!

enter image description here enter image description here

Illuminate\Database\QueryException SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cms.statuses' doesn't exist (SQL: insert into statuses (status, user_id, updated_at, created_at) values (dfg, 1, 2021-06-22 15:16:10, 2021-06-22 15:16:10))

Here is my Table schema

enter image description here

And HomeController function

enter image description here

I would be very happy if anyone can explain, why I am getting this kind of error when I'm not using this line => protected $table='status'; thank you!

Answer

Solution:

To understand better:

Each model is extended from use Illuminate\Database\Eloquent\Model; So it has method called getTable() like below

/**
  * Get the table associated with the model.
  *
  * @return string
  */
  public function getTable()
  {
     return $this->table ?? Str::snake(Str::pluralStudly(class_basename($this)));
  }

if you see this:

  1. First it will check if you have set table property manually. If its then it will take table name from protected $table='status';

  2. Suppose if you haven't set property then it will take class name and then pluralize the name then it will convert to snake case.

For example in your case, Model name is Status

$response=Str::snake(Str::pluralStudly(class_basename("Status")));
dd($response);

Result will be statuses. So it expect statuses table in database.

For example Consider You have model name UserDetail then

$response=Str::snake(Str::pluralStudly(class_basename("UserDetail")));
dd($response);

then result will be user_details. So it expect user_details table in database.

Source