Home > Back-end >  One To Many Polymorphic relationship - Call to undefined method getConnectionName()
One To Many Polymorphic relationship - Call to undefined method getConnectionName()

Time:07-16

I have followed the documentation very closely but something isn't working
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.

CodePudding user response:

i think you just changes model File class name, it's ok. because this model's name conflict with Facades File class name

CodePudding user response:

Thanks to Noah and Erik for their answers. You both helped me find what was needed which was the following site. enter image description here All fixed now, thanks

  • Related