被关联表模型中写关联
xuexi 2025-08-03 16:17:37 发表在:PHP 查看数:71
模型中
 public function neicun(){

    return $this->hasMany(Neicun::class, 'shebei_id');
      }
控制器中
$this->neicun->first()?->daxiao



// 获取所有关联记录的daxiao属性数组
$daxiaoList = $this->neicun->pluck('daxiao')->toArray();

/ 获取第一个关联记录的daxiao
$firstDaxiao = $this->neicun->first()->daxiao;
// 建议加上判断避免空错误
$firstDaxiao = $this->neicun->first()?->daxiao;
最近访问时间:2025-08-18 01:25:22
知识库:372条鸣谢:TAY  备案号:蜀ICP备2024090044号-2