Hello Laracasts Support,
I am encountering an issue while running a migration in my Laravel 9.11.0 project. The migration uses a trait, but I am receiving a fatal error indicating that the trait is not found. Below are the details of the problem.
Error Message:
PHP Fatal error: Trait "database\EloquentTableValuesTrait" not found in /home/boto/www/franko-portal/database/migrations/2024_06_17_131137_create_project_customer_favorite_events_table.php on line 10
File Structure:
project_root/
|-- app/
|-- database/
| |-- EloquentTableValuesTrait.php
| |-- migrations/
| |-- 2024_06_17_131137_create_project_customer_favorite_events_table.php
|-- vendor/
Trait Definition:
<?php
namespace Database;
use Illuminate\Database\Schema\Blueprint;
trait EloquentTableValuesTrait
{
/**
* @param Blueprint $table
* @param object $eloquent
* @param string|null $column
* @return void
*/
function foreignFor(Blueprint $table, object $eloquent, ?string $column = null): void
{
$table->foreignIdFor($eloquent::class, $column)->nullable();
$table->foreign($column ?? $eloquent::columnForForeignKey())->references($eloquent::idColumnName())->on($eloquent::tableName());
}
}
Migration File:
<?php
use App\Models\Bot\Customer;
use App\Models\Project\Event;
use Database\EloquentTableValuesTrait;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
use EloquentTableValuesTrait;
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('project_customer_favorite_events', function (Blueprint $table) {
$customer = new Customer();
$this->foreignFor($table, $customer);
$event = new Event();
$this->foreignFor($table, $event);
$table->primary([$customer->getForeignKey(), $event->getForeignKey()]);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('project_customer_favorite_events');
}
};
Steps Taken:
- Verified that EloquentTableValuesTrait.php is located in the database directory.
- Ensured the namespace in EloquentTableValuesTrait.php is Database.
- Used the trait in the migration file correctly.
- Ran composer dump-autoload to refresh the autoload files.
- Attempted to run the migration again with php artisan migrate.
Despite these steps, the error persists in Laravel 9.11.0.
Is this normal behavior or is it some kind of bug?