earmsby's avatar

problem creating relation manager

I have this model:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AltWorkId extends Model
{
    protected $fillable = [
        'work_id',
        'alt_work_entity_id',
        'alt_id_number'
    ];

    public function work(): BelongsTo
    {
        return $this->belongsTo(Work::class);
    }
    public function altWorkEntity(): BelongsTo
    {
        return $this->belongsTo(AltWorkIdEntity::class);
    }
}

My Work model has this:

    public function altWorkIds(): HasMany
    {
        return $this->hasMany(AltWorkId::class);
    }

I'm trying to create a relation manager on my WorkResource. When I try to create it via php artisan:

php artisan make:filament-relation-manager WorkResource 

It asks me for the relationship, the related model and the title attribute, but then comes back saying:

 ErrorException 

  class_parents(): Class AltWorkId does not exist and could not be loaded

I don't understand why it thinks the AltWorkId model doesn't exist. It's definitely there in the Models directory where it should be. Any ideas?

0 likes
2 replies
LaryAI's avatar
Level 58

Certainly! The error message:

ErrorException 
  class_parents(): Class AltWorkId does not exist and could not be loaded

suggests that Laravel (or Filament) is not able to auto-discover or autoload your AltWorkId model class. This is a common issue with namespace, autoloading, or typo problems.

Here’s how to debug and solve it:


1. Check File Name & Location

  • Make sure the file is named exactly AltWorkId.php.
  • Double-check it’s under app/Models/AltWorkId.php.

2. Check Namespace & Class Name

Your model should start with:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AltWorkId extends Model
{
    // ...
}

Both the namespace and the class must match exactly with your folder structure and filename.


3. Composer Autoload

If you recently created the file, you may need to refresh Composer’s autoloader. Run:

composer dump-autoload

4. References in Your Code

Anywhere you reference AltWorkId (like in your Work model), make sure you import the class properly:

use App\Models\AltWorkId;

5. Filament Command Input

When prompted by php artisan make:filament-relation-manager WorkResource,

  • For relationship, type: altWorkIds (name of the relationship in your Work model).
  • For related model, enter either: App\Models\AltWorkId or AltWorkId (the prompt sometimes differs depending on Filament/Laravel version).
  • For title attribute, likely something like id or alt_id_number.

6. Case Sensitivity

On Linux/macOS, file and directory names are case sensitive. Make sure you don't have altworkid.php or Altworkid.php — must be AltWorkId.php.


7. Restart Your Code Editor/IDE

Sometimes IDEs may cache results. Restart to make sure there are no stale caches.


In summary:

  • Confirm file name and namespace.
  • Run composer dump-autoload.
  • Double-check relationship and import statements.
  • Use the correct model name when prompted.

Example: Clean Model File

app/Models/AltWorkId.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AltWorkId extends Model
{
    protected $fillable = [
        'work_id',
        'alt_work_entity_id',
        'alt_id_number'
    ];

    public function work(): BelongsTo
    {
        return $this->belongsTo(Work::class);
    }

    public function altWorkEntity(): BelongsTo
    {
        return $this->belongsTo(AltWorkIdEntity::class);
    }
}

Finally: Try Again

After following those steps, try running your Filament command again.


If you still experience issues:
Post back with your folder structure and the exact command and answers you provide to each prompt—there may be a typo or subtle issue.


Summary:
Model not found errors almost always point to file naming, namespace, typo, or autoloading problems. Carefully checking these will usually resolve the issue.

earmsby's avatar

I checked everything mentioned, ran the composer command and restarted my IDE. Still got the same result. Tried one more time and this time answered "NO" to "Should the configuration be generated from the current database columns?" and now the relation manager was generated.

Please or to participate in this conversation.