根据 Laravel 的文档在我创建的模型中创建关系时出现了我的问题。
我有我的区号表和我的电话表,一个区号可以属于多部电话,但一部电话只有一个区号,这使我成为一对多的关系
我将向您展示我的模型和它们之间的关系。
电话.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');
}
区号.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');
}
在我的控制器中,调用如下
GenerateNumbersController.php
$phones = Phone::where('DATE',$searchOrCreate)
->chunk(500,function($phone){
foreach ($phone as $p) {
dd($p->areacodes);
}
});
searchOrCreate 变量有一个日期格式的值,我对 $ phone 做了一个 dd 并且它显示的所有关系都是空的
慕沐林林
猛跑小猪