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

AKaramanec's avatar

Can`t add Trait to migration in my Laravel 9.11.0 project

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:

  1. Verified that EloquentTableValuesTrait.php is located in the database directory.
  2. Ensured the namespace in EloquentTableValuesTrait.php is Database.
  3. Used the trait in the migration file correctly.
  4. Ran composer dump-autoload to refresh the autoload files.
  5. 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?

0 likes
4 replies
Nakov's avatar

If you are adding a class in the root of your database folder, you will have to add the namespace to the composer.json file, it should look something like this: (note the last line)

"autoload": {
        "psr-4": {
            "App\": "app/",
            "Database\Factories\": "database/factories/",
            "Database\Seeders\": "database/seeders/",
            "Database\": "database/" 
        },
...

and run composer dump-autoload after you add it.

1 like
AKaramanec's avatar

@Nakov Thank you very much for your prompt and helpful response! Your assistance is greatly appreciated.

s4muel's avatar
s4muel
Best Answer
Level 50

move your trait to a Traits subfolder (no necessary, but database namespace is bit of too generic and there are two more namespaces inside, already, but it is your call) and change the composer.json, so the trait class is autoloaded into correct namespace

 "autoload": {
    "psr-4": {
      "App\": "app/",
      "Database\Factories\": "database/factories/",
      "Database\Seeders\": "database/seeders/"
      "Database\Traits\": "database/traits/"
    }
  },

run composer dump-autoload afterwards and it should work

1 like
AKaramanec's avatar

@s4muel Thank you very much for your prompt and helpful response! Your assistance is greatly appreciated. I really liked your answer!

1 like

Please or to participate in this conversation.