D
D
Dimon3x2019-01-02 20:12:31
Laravel
Dimon3x, 2019-01-02 20:12:31

Where to use one-to-one polymorphic relationships?

I did the One To One (Polymorphic) dock, it works, but I haven’t entered it yet, when to use it, give me simple tasks so that I can fully figure it out for One To One (Polymorphic).
There are no examples in the dock at all.
I made 1 example, that's what happened, am I right in general? post
model

<?php
 
namespace App;
 
use Illuminate\Database\Eloquent\Model;
 
class Post extends Model
{
     public function image()
    {
        return $this->morphOne('App\Image', 'imageable');
    }
}

Polymorphic table
555-jpg.3370posts
555-jpg.3371
$post = Post::find(1);
$image = $post->image;
   
dump($post);
dump(User::find($post->user_id));
dump($image);

Displays a collection with an article, a collection with a user and a collection with these images, and then I will put the necessary data into the variables
and pass them to the view and arrange the images in media queries, did I correctly understand the essence of this one-to-one polymorphic relationship ??

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question