3 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
我知道這是某種預(yù)期,但嘗試返回一個(gè)空關(guān)系?
public function sub()
{
? ? switch($this->sub_type) {
? ? ? ? case 'a':
? ? ? ? ? ? return $this->hasOne('SubTypeA');
? ? ? ? ? ? break;
? ? ? ? case 'b':
? ? ? ? ? ? return $this->hasOne('SubTypeB');
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? return $this->newQuery();? // or newQueryWithoutScopes()
? ? }
}
它應(yīng)該防止 addEagerConstraints() 對(duì) null 的錯(cuò)誤。

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
我想指出的幾件事是,您的關(guān)系不應(yīng)該是有條件的,在將默認(rèn)關(guān)系設(shè)置為您想要的默認(rèn)關(guān)系之后,您可以定義范圍并管理內(nèi)容或使用withDefault在失敗情況下返回某些內(nèi)容。
關(guān)于在 null 上調(diào)用成員函數(shù)的錯(cuò)誤:- 下面是另一個(gè)示例
<?php
class MyClass{
? ? function bar(){
? ? ? ? echo "something";
? ? }
}
class MyAnotherClass{
? ? function foo(){
? ? ? ? if (1>2) {
? ? ? ? ? ? $obj = new MyClass();
? ? ? ? ? ? $obj->x = $x;
? ? ? ? ? ? $obj->y = $y;
? ? ? ? ? ? $obj->save();
? ? ? ? ? ? return $obj;
? ? ? ? } else {
? ? ? ? ? ? return null;
? ? ? ? }
? ? }
}
$myAnotherObj = new MyAnotherClass();
$myClass = $myAnotherObj->foo();
$myClass->bar()
?>
與其這樣做,我更愿意拋出異常并處理它,這樣我就能得到失敗的具體原因,在 Laravel救援輔助函數(shù)中你可以選擇使用。
<?php
class MyClass{
? ? function bar(){
? ? ? ? echo "something";
? ? }
}
class MyAnotherClass{
? ? function foo(){
? ? ? ? if (1>2) {
? ? ? ? ? ? $obj = new MyClass();
? ? ? ? ? ? $obj->x = $x;
? ? ? ? ? ? $obj->y = $y;
? ? ? ? ? ? $obj->save();
? ? ? ? ? ? return $obj;
? ? ? ? } else {
? ? ? ? ? ? throw new Exception("could not create my class object", 100); // Better to create custom exception class here
? ? ? ? }
? ? }
}
$myAnotherObj = new MyAnotherClass();
try {
? ? $myClass = $myAnotherObj->foo();
? ? $myClass->bar();
} catch(Exception $e) {
? ? echo $e->getMessage();
}
?>
如果對(duì)我來說數(shù)據(jù)不是那么重要我會(huì)考慮創(chuàng)建一個(gè)空對(duì)象
<?php
? ? class MyClass{
? ? ? ? function bar(){
? ? ? ? ? ? echo "something";
? ? ? ? }
? ? }
? ??
? ? class MyAnotherClass{
? ? ? ? function foo(){
? ? ? ? ? ? $obj = new MyClass();
? ? ? ? ? ? if (1>2) {
? ? ? ? ? ? ? ? $obj->x = $x;
? ? ? ? ? ? ? ? $obj->y = $y;
? ? ? ? ? ? ? ? $obj->save();??
? ? ? ? ? ? }?
? ? ? ? ? ? return $obj;
? ? ? ? }
? ? }
? ??
? ? $myAnotherObj = new MyAnotherClass();
? ? $myClass = $myAnotherObj->foo();
? ? $myClass->bar()
? ?>
但是如果您正在使用該對(duì)象屬性進(jìn)行操作,那么屬性將為 null 而不是對(duì)象,因此根據(jù)您在使用它時(shí)的紀(jì)律,您可以做出決定。
我想如何處理你的情況?
異常類
<?php
namespace App\Exceptions;
use Exception;
class SubTypeNotFound extends Exception {
? ? public function report()
? ? {
? ? ? ? \Log::debug('Could not find this subtype');
? ? }
}
?>
模型類
<?php
class Mains extends Model
{
? ? public function subA()
? ? {
? ? ? ? return $this->hasOne(SubTypeA::class);
? ? }? ??
? ? public function subB()
? ? {
? ? ? ? return $this->hasOne(SubTypeB::class);
? ? }
? ? public function scopeSub($query, $type)
? ? {
? ? ? ? return $query
? ? ? ? ? ->when($type === 'a',function($q){
? ? ? ? ? ? ? return $q->with('subA');
? ? ? ? ?})
? ? ? ? ?->when($type === 'b',function($q){
? ? ? ? ? ? ? return $q->with('subB');
? ? ? ? ?}),function($q){
? ? ? ? ? ? ?throw SubTypeNotFound();
? ? ? ? ?});
? ? }
}
?>
檢索它時(shí)
try {
? ? $sub = Mains::sub('a')->get();
} catch(SubTypeNotFound $e) {
? ? return $e->getMessage();
}
如果有,$this->sub_type您可以避免使用該type參數(shù)。

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以簡單地將 if 條件放在 switch case 之前。
public function sub()
{
if($this->sub_type){
switch($this->sub_type) {
case 'a':
return $this->hasOne('SubTypeA');
break;
case 'b':
return $this->hasOne('SubTypeB');
break;
default:
return null;
}
}else{
return null
}
}
- 3 回答
- 0 關(guān)注
- 177 瀏覽
添加回答
舉報(bào)