I'm encountering a weird issue with Eloquent. In short, I have a model, which is called Jobrole. This describes a role in a company.
The model for this (jobrole.php) is:
<?php
namespace App\Models;
use \Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Jobrole extends Model
{
use HasFactory, hasUuids;
protected $guarded = [];
}
The migration file is below:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('jobroles', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('owner_id');
$table->foreign('owner_id')->references('id')->on('workplaces')->onDelete('cascade');
$table->uuid('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('name');
$table->string('description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('jobroles');
}
};
It's important to note that I have not changed the database name in the migration.
When hitting the store method on the controller with some data, I receive the following error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'shiftlog.job-roles' doesn't exist
Here's where it gets interesting. If I change the name of the table, via a migration, I receive the exact same error, only with the new table name! So depending on which table name I use, it complains that the other isn't found. Now I'll receive the error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'shiftlog.job_roles' doesn't exist
I'm really at a loss as to why this is occurring. I've tried composer -o dump-autoload, and I've tried manually altering the table name in table plus, and also using $protected table = 'tablename' in the model which is ignored.
It might be interesting to note that the model name was originally JobRole, but I thought the capital R might have been causing issues so I've deleted the model, and regenerated it using
php artisan make:model Jobrole -a
Which does not seem to have made a difference.
Any help would be much appreciated, thank you.