doutidi5037 2019-03-21 20:12
浏览 67
已采纳

Laravel - 多态关系不起作用

So I have the following code:

class PageSection extends Model {
    protected $table = "PageSection";

    const TYPE_CURATED = 0;
    const TYPE_AUTOMATED = 1;

    public function list() {
        return $this->morphTo('list', 'entity_type', 'id_Entity');
    }
}

then in AppServiceProvider.php I have the following:

use App\PageSection;
use App\PageSectionGroup;
use App\PageListEntry;
use App\RSSFeed;
use App\Shortcut;
use App\RSSEpisode;
use App\PageList;
use App\AutomatedList;


class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        Relation::morphMap([
            'Section'                    => PageSection::class,  
            'SectionGroup'               => PageSectionGroup::class,
            PageSection::TYPE_CURATED    => PageList::class,
            PageSection::TYPE_AUTOMATED  => AutomatedList::class,
            PageListEntry::TYPE_FEED     => RSSFeed::class,
            PageListEntry::TYPE_SHORTCUT => Shortcut::class,
            PageListEntry::TYPE_EPISODE  => RSSEpisode::class
        ]);

    }

Then I have a test route in my api routes that checks to see if the list is being loaded, and it returns null: (Yes, I've verified that the section itself exists)

Route::get('/test', function() {
    $section = PageSection::with(['list', 'type'])->find(1);

    // this returns null
    return $section->list;
});

My database schema for PageSection is such that entity_type tells what the model is, and id_Entity is the foreign key for that model, which is named 'id' on the referenced table.

The other relations defined in morphMap are working properly, yet for some reason the list() relationship in PageSection is not. I'm not sure what I'm doing wrong here.. any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • duanjianshen4871 2019-03-21 21:11
    关注

    Ok, so I figured out what was going on. It's probably a bug with Laravel's morphMap. I was using 0 for the PageSection::TYPE_CURATED constant, which is a falsey value. When I switched to:

    Relation::morphMap([
        'PageList'                   => PageList::class,
        'AutomatedList'              => AutomatedList::class,
        'Section'                    => PageSection::class,  
        'SectionGroup'               => PageSectionGroup::class,
        PageListEntry::TYPE_FEED     => RSSFeed::class,
        PageListEntry::TYPE_SHORTCUT => Shortcut::class,
        PageListEntry::TYPE_EPISODE  => RSSEpisode::class
    ]);
    

    it all worked fine. Seems like Laravel doesn't like the value 0.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?