鍍金池/ 問(wèn)答/PHP  網(wǎng)絡(luò)安全/ laravel 自定義多態(tài)關(guān)聯(lián)的類型字段 string 轉(zhuǎn) int 的問(wèn)題?

laravel 自定義多態(tài)關(guān)聯(lián)的類型字段 string 轉(zhuǎn) int 的問(wèn)題?

自定義多態(tài)關(guān)聯(lián)的類型字段
寫(xiě)映射

[
1=>'App\Post',
2=>'App\Commit',
]

但是我有多個(gè)表是多對(duì)多關(guān)系呢?

需要另一套映射

[
1=>'App\Other',
2=>'App\User',
]

多態(tài)關(guān)聯(lián)的表類型都用的int,但是1在不同的model中映射的不同的表,這樣該怎么做呢?

  public function click()
    {
        if ($this->type == 1) {
            return $this->belongsTo('App\Models\HomeList', 'click_id');
        }
        if ($this->type == 2) {
            return $this->belongsTo('App\Models\SlideShow', 'click_id');
        }
        if ($this->type == 3) {
            return $this->belongsTo('App\Models\ReleaseTask', 'click_id');
        }

        return $this->belongsTo('App\Models\HomeList', 'click_id');
    }

上面的代碼結(jié)果 $this->type這個(gè)值解析不出來(lái),一個(gè)if都沒(méi)進(jìn)?。?!
我該怎么辦?

回答
編輯回答
夏木

你是怎么調(diào)用這個(gè)click方法的,打印$this->type是沒(méi)有值的嗎?

2017年12月31日 08:37