For getting every new tutorial link Please join our telegram group

Part 7 laravel one to one polymorphic relationship by morphOne() method with hindi video

In this laravel tutorial we will discuss about eloquent model one to one polymorphic relationship by morphOne() method tutorial with hindi video.


Also ask laravel morphTo() relationship tutorial, one to one polymorphic relationship not working, how to get data by morphTo() relationship.

Laravel one to one polymorphic relationship by morphOne()

Polymorphic relationship define when one child table connect to many other tables. one-to-one polymorphic relationship work same as one-to-one relationship but here child model relate to more than models.

Like an example of one-to-one polymorphic relationship Let posts and categories have a common table for storing images. one-to-one relationship use morphTo() and morphOne() method in model for define relationship.

Table structure of one-to-one polymorphic relationship

Tables
posts
    id - integer
    name - string

categories
    id - integer
    name - string

images
    id - integer
    url - string
    imageable_id - integer
    imageable_type - string

here common child table is images that contain two column imageable_id and imageable_type. Here imageable_id is integer type and contain id value of posts and caterories, and imageable_type is string type column which is determine type of model that contain model path like App\Models\Post or App\Models\Category.

Model structure of one-to-one polymorphic relationship

Image Model

In Image model we define a imageable method which contain morphTo() relationship and it get image parent model data like post and category.

class Image extends Model
{
    /**
     * Get the parent imageable model (category or post).
     */
    public function imageable() {
        return $this->morphTo();
    }
}
Post Model

In post model we define a image method which content a morpOne() method and its first argument have child Model name 'Image' and second argumeny have method name of child model 'imageable'. When we save post image in images table than model path App\Models\Post save in imageable_type attribute.

class Post extends Model
{
    /**
     * Get the post's image.
     */
    public function image() {
        return $this->morphOne(Image::class, 'imageable');
    }
}
Category Model

Category model also have a image method for getting category image and it same working as Post model.

class Category extends Model
{
    /**
     * Get the category's image.
     */
    public function image() {
        return $this->morphOne(Image::class, 'imageable');
    }
}

One-to-one relationship data return in controller

Once models and tables are define than use relationships throw models. For example retrieve Category image in Category controller.

use App\Models\Category;

$category = Category::find(1);

$image = $category->image;

You have retrieve parent of polymorphic model by name of method which is define in child model. Like we get an image parent data. it return post or category data depend on type column.

use App\Models\Image;

$image = Image::find(1);

$imageable = $image->imageable;

Customize column names in one-to-one polymorphic relationship

When our child table images column names imageable_id and imageable_type name customize than we define new name in second and third arument and first argument contail __FUNCTION__.

public function imageable(){
    return $this->morphTo(__FUNCTION__, 'imageable_type', 'imageable_id');
}

You read this laravel tutorial on advanced web tutorial. here we provide laravel beginners to advanced tutorial in hindi video.

php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Related tutorial links