本文介绍了如何为不同的类使用Relation :: morphMap()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel多态关系.我为两个目的定义了两个morphTo关系.我的问题是,但是当我定义Relation :: morphMap()函数array的键时,那么我的数组键在一种情况下是相同的,所以我想知道有什么方法可以指定我要定义的特定类别的关系.

I am using laravel polymorphic relation.I have defined two morphTo relations for two purpose.My question is that ,but when I am defining the key of Relation::morphMap() function array , then my array key is same for one case, so I want to know is there any way by which I can specify that I am defining relation for specific class.

我的初恋....

Package.php

Package.php

public function provider()
{
    return $this->morphTo(null, 'map_type_id', 'map_id');
}

Venue.php

Venue.php

public function packages()
{
    return $this->morphMany(VendorPackage::class, 'map', 'map_type_id', 'map_id');
}

Vendor.php

Vendor.php

public function packages()
{
    return $this->morphMany(VendorPackage::class, null, 'map_type_id', 'map_id');
}

我想设置要与 map_type_id 进行比较的密钥,所以我要在服务提供商中设置密钥.

I want to set the key to compare with map_type_id so I am setting the key in service provider.

Relation::morphMap([
                           config('evibe.roles.planner')        => \Vendor::class,
                           config('evibe.roles.artist')         => \Vendor::class,
                           config('evibe.roles.venue')          => \Venue::class,
                       ], false);

我的第二个morphTo关系

Ticket Booking.php

Ticket Booking.php

public function provider()
{
    return $this->morphTo(null, 'map_type_id', 'map_id');
}

Venue.php

Venue.php

public function bookings()
{
    return $this->morphMany(TicketBooking::class,null,'map_type_id','map_id');
}

Decors.php

Decors.php

public function bookings()
{
    return $this->morphMany(TicketBooking::class,null,'map_type_id','map_id ');
}

再次,我必须在服务提供者中定义morphTo,因为我没有使用默认的模型名称.

Now again I have to define the morphTo in service provider because I am not using the default Model name.

所以我在服务提供商中的morphTo变成了这样.

so my morphTo in service providers became like this.

Relation::morphMap([
                           config('evibe.roles.planner')        => \Vendor::class,
                           config('evibe.roles.artist')         => \Vendor::class,
                           config('evibe.roles.venue')          => \Venue::class,
                           config('evibe.ticket_type.venues')   => \Venue::class,
                           config('evibe.ticket_type.decors')   => \Decor::class
                       ], false);

现在我的问题是键 config('evibe.roles.planner') config('evibe.ticket_type.venues)具有相同的值3,因此当通过关系访问两者时,则会抛出错误,因为数组具有相同的键.

Now my problem is that key config('evibe.roles.planner') and config('evibe.ticket_type.venues) has the same value 3, so when both things is accessed by the relationship then it is throwing error, because array have same key.

所以我想问问是否还有其他方法可以为不同的关系定义不同的morphMap.

So I want to ask is there any other way to define different morphMap for different relationship.

推荐答案

让我们从定义多态关系开始

Lets start by defining the polymorphic relations

初恋....

Package.php

public function provider() {
  return $this->morphTo(null,  'map_type_id', 'map_id');
}

Venue.php

public function packages() {
  // you should provide the relation name, in our exemple its called `provider` as a second parameter
  return $this->morphMany(VendorPackage::class, 'provider', 'venues');
}

Vendor.php

public function packages() {
  // you should provide the relation name, in our exemple its called `provider` as a second parameter
  return $this->morphMany(VendorPackage::class, 'provider', 'vendors');
}

第二关系

TicketBooking.php

public function provider() {
  return $this->morphTo(null, 'map_type_id', 'map_id');
}

Venue.php

public function bookings() {
  return $this->morphMany(TicketBooking::class, 'provider', 'venues');
}

Decors.php

public function bookings() {
  return $this->morphMany(TicketBooking::class, 'provider', 'decors');
}

并将Relation::morphMap注册为

Relation::morphMap([
  'vendors' => \Vendor::class,
  'venues' =>  \Venue::class,
  'decors' =>  \Decor::class
]);

这篇关于如何为不同的类使用Relation :: morphMap()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 16:43
查看更多