A
A
Artem00712018-07-13 00:15:03
Laravel
Artem0071, 2018-07-13 00:15:03

Why doesn't morphTo work?

The database has a table of files:

Schema::create('files', function (Blueprint $table) {
            $table->uuid('id')->primary();

            $table->uuid('fileable_id');
            $table->string('fileable_type');

            $table->timestamps();
        });

And the pictures themselves:
Schema::create('images', function (Blueprint $table) {
            $table->uuid('id')->primary();

            $table->timestamps();
        });

File class:
class File extends Model
{
    public function fileable()
    {
        $this->morphTo();
    }

Image class:
class Image extends Model
{
    public function file()
    {
        return $this->morphOne(File::class, 'fileable');
    }

This code works: $image->file
But this code doesn't work: It $file->fileable
throws an error:
"message" => "App\Models\File\File::fileable must return a relationship instance."

And $file->fileable()it returns null. But after all, the file can be found through the picture ... I
have already broken my whole brain, what combinations I didn’t do, what I just didn’t insert into morphTo();
Where is the mistake here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2018-07-13
@Artem0071

I recommend to sleep, because you already have return's lost in the code...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question