hasOne(ViolationLaw::class, 'violationcode', 'violationcode'); } public function getLawTypeAttribute() { // 取得陣列 $violationtype = $this->violationtype; // $violationtype = []; if ($violationtype == "未依標誌標線號誌" || $violationtype == "未依標誌標線行駛" || $violationtype == "機車未依規定兩段式左轉") { $violationtype = ["未依標誌標線號誌行駛"]; } else if ($violationtype == "闖紅燈") { $violationtype = ["闖紅燈", "紅燈右轉", "紅燈越線"]; } else if ($violationtype == "未禮讓行人") { $violationtype = ["未禮讓行人"]; } else if ($violationtype == "違規停車") { $violationtype = ["違規停車"]; } else if ($violationtype == "未保持路口淨空") { $violationtype = ["未保持路口淨空"]; } else if ($violationtype == "超速") { $violationtype = ["超速"]; } else { $violationtype = [$violationtype]; } $types = ViolationLaw::whereIn('type', $violationtype)->get(); $lawData = []; foreach ($types as $type) { $lawData["$type->violationcode"] = "[$type->violationcode] $type->display_name"; } return $lawData; } }