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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示