再會。我在我維護的代碼中看到了這個片段。我試圖理解它的含義(就像我什至必須向某人解釋它一樣)。誰能幫忙簡化?代碼如下所示。
public function pullFrom(string $appType)
{
switch ($appType) {
case 'personal':
case 'plugin':
return $this->belongsTo(PersonalUser::class, 'local_id')->first();
default:
throw new \Exception('Invalid user type provided', Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
是不是和說一樣 $user->pullFrom($app_type)->first();
uj5u.com熱心網友回復:
我認為您應該默認情況下從belongsTo 關系中洗掉 ->first() 關系,belongsTo 僅回傳與它相關的類的 1 個元素,并將其從您執行的呼叫中洗掉:
public function pullFrom(string $appType)
{
switch ($appType) {
case 'personal':
case 'plugin':
return $this->belongsTo(PersonalUser::class, 'local_id');
default:
throw new \Exception('Invalid user type provided', Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/379636.html
上一篇:刀片中的Laravel8路由
