根據(jù) Laravel 的文檔在我創(chuàng)建的模型中創(chuàng)建關(guān)系時(shí)出現(xiàn)了我的問(wèn)題。我有我的區(qū)號(hào)表和我的電話表,一個(gè)區(qū)號(hào)可以屬于多部電話,但一部電話只有一個(gè)區(qū)號(hào),這使我成為一對(duì)多的關(guān)系我將向您展示我的模型和它們之間的關(guān)系。電話.php/** * The table associated with the model. * * @var string * @author Luis Morales */protected $table = 'PHONES';/** * The primary key associated with the table. * * @var string * @author Luis Morales */protected $primaryKey = 'PHONES_ID';/** * The attributes that are mass assignable. * * @var array * @author Luis Morales */protected $fillable = [ 'AREACODES_ID', 'PHONE', 'DATE', 'USERS_ID',];/** * Indicates if the model should be timestamped. * * @var bool * @author Luis Morales */public $timestamps = false;/** * Get the user for the phone * * @author Luis Morales */public function users(){ return $this->belongsTo('App\User');}/** * Get the area code for the phone * * @author Luis Morales */public function areacodes(){ return $this->belongsTo('App\AreaCode');} 區(qū)號(hào).php/** * The table associated with the model. * * @var string * @author Luis Morales */protected $table = 'AREACODES';/** * The primary key associated with the table. * * @var string * @author Luis Morales */protected $primaryKey = 'AREACODES_ID';/** * The attributes that are mass assignable. * * @var array * @author Luis Morales */protected $fillable = [ 'CODE', 'CITY', 'STATESZONE_ID',];/** * Indicates if the model should be timestamped. * * @var bool * @author Luis Morales */public $timestamps = false;/** * Get the phone for the area codes * * @author Luis Morales */public function phones(){ return $this->hasMany('App\Phone');}在我的控制器中,調(diào)用如下GenerateNumbersController.php$phones = Phone::where('DATE',$searchOrCreate) ->chunk(500,function($phone){ foreach ($phone as $p) { dd($p->areacodes); } });searchOrCreate 變量有一個(gè)日期格式的值,我對(duì) $ phone 做了一個(gè) dd 并且它顯示的所有關(guān)系都是空的
2 回答

慕沐林林
TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊
你調(diào)用了錯(cuò)誤的方法。dd($p->區(qū)號(hào)); 因?yàn)樵谀氖謾C(jī)型號(hào)中,您的型號(hào)名稱是與您調(diào)用的功能不匹配的區(qū)號(hào)。解決方案更改 dd($p->areacode);
為dd($p->areacodes)
;

猛跑小豬
TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
更改您的 return 語(yǔ)句以 return $this->belongsTo( 'App\AreaCode', 'AREACODES_ID', 'PHONES_ID');
- 2 回答
- 0 關(guān)注
- 162 瀏覽
添加回答
舉報(bào)
0/150
提交
取消