laraveleloquentpolymorphic-associationslaravel-9

One To Many Polymorphic relationship - Call to undefined method getConnectionName()


I have followed the documentation very closely but something isn't working
https://laravel.com/docs/9.x/eloquent-relationships#one-to-many-polymorphic-relations

I am trying to add permissions into my code which will give a User access to a File or a Folder. I understand this needs a one-to-many polymorphic relationship as each Permission has one permissionable, while each File or Folder might have many permissions.
$table->morphs('permissionable'); in a migration adds the permissionable_type(string) and permissionable_id(integer) columns to the permissions table
phpMyAdmin Screenshot

The Permission model has been created with the relevant fillable columns and the permissionable method containing a morphTo():


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

use App\Models\File;

class Permission extends Model
{
    use HasFactory, SoftDeletes;
    protected $fillable = [
        'user_id',
        'permissionable_type',
        'permissionable_id',
        'privilege',
    ];
    /**
     * Get the parent object (file or folder).
     */
    public function permissionable()
    {
        return $this->morphTo();
    }
}

The Folder and File models have both had the following methods added:

    public function permissions()
    {
        return $this->morphMany(Permission::class, 'permissionable');
    }

A Permission is going to be created which will share a File with a User
File
Permission
And then the Permission is found and it's permissionable is requested
dd(Permission::find(1)->permissionable);

Except this is where the area happens:
enter image description here

I have tried to follow the documentation religiously and all the answers I see online just say to check namespace or ensure that all Models have an extends Model which I have already done. Let me know if there is any more information I need to provide, thanks in advance.


Solution

  • Thanks to Noah and Erik for their answers. You both helped me find the solution which was on the following site. https://laracasts.com/discuss/channels/general-discussion/polymorphic-relations-gives-class-staff-not-found
    The types need to include the paths in the database
    enter image description here