Php laravelmorpto返回Null
我使用的是Laravel5.4,很难弄清楚为什么我的Morpto关系无论做什么都会返回null。关系的倒数是可以的,但是当我试图检索多态关系的所有者时,它是空的Php laravelmorpto返回Null,php,laravel,eloquent,Php,Laravel,Eloquent,我使用的是Laravel5.4,很难弄清楚为什么我的Morpto关系无论做什么都会返回null。关系的倒数是可以的,但是当我试图检索多态关系的所有者时,它是空的 class Opportunity extends Model { public function Organization() { return $this->morphTo('Organization'); } } class Account extends model { pu
class Opportunity extends Model {
public function Organization() {
return $this->morphTo('Organization');
}
}
class Account extends model {
public function Opportunities() {
return $this->morphMany('App\Opportunity', 'Organization');
}
}
class Department extends model {
public function Opportunities() {
return $this->morphMany('App\Opportunity', 'Organization');
}
}
$org = App\Opportunity::findOrFail(1)->Organization;
完整名称空间存储在数据库中,_type和_id实际上在列中具有适当的组织类型和id(即“App\Account”和“456”)。因此,我知道数据库记录和返回的Opportunity对象在列中具有正确的组织(我可以在数据库中正确地看到它),但无论我做什么,如果我尝试检索组织,它都是空的
class Opportunity extends Model {
public function Organization() {
return $this->morphTo('Organization');
}
}
class Account extends model {
public function Opportunities() {
return $this->morphMany('App\Opportunity', 'Organization');
}
}
class Department extends model {
public function Opportunities() {
return $this->morphMany('App\Opportunity', 'Organization');
}
}
$org = App\Opportunity::findOrFail(1)->Organization;
这是输出。您会注意到该组织位于属性中,但该关系为null,我无法让它返回,甚至在查询中添加->with('Organization')。它甚至似乎没有执行查询来获取所有者
#primaryKey: "ID"
+timestamps: true
#guarded: []
#hidden: []
#visible: []
#with: []
#dissociateRelations: []
#connection: null
#keyType: "int"
+incrementing: true
#perPage: 15
+exists: true
+wasRecentlyCreated: true
#attributes: array:13 [
"StageID" => 12
"TypeID" => 1
"OriginID" => 20
"Description" => "Interested in scanner fi6140"
"UserID" => 3
"SolutionValue" => ".00"
"MarginValue" => ".00"
"created_at" => "2010-09-16 11:19:00.000"
"updated_at" => "2015-09-01 12:32:00.000"
"_migrationID" => "4299"
"Organization_type" => "App\Account"
"Organization_id" => 456
"ID" => 1
]
#original: array:13 [
"StageID" => 12
"TypeID" => 1
"OriginID" => 20
"Description" => "Interested in scanner fi6140"
"UserID" => 3
"SolutionValue" => ".00"
"MarginValue" => ".00"
"created_at" => "2010-09-16 11:19:00.000"
"updated_at" => "2015-09-01 12:32:00.000"
"_migrationID" => "4299"
"Organization_type" => "App\Account"
"Organization_id" => 456
"ID" => 1
]
#dateFormat: null
#events: []
#observables: []
#relations: array:3 [
"Organization" => null
"Projects" => Illuminate\Database\Eloquent\Collection {#3463
#items: []
}
"Tickets" => Illuminate\Database\Eloquent\Collection {#3443
#items: []
}
]
#touches: []
#forceDeleting: false
所以,看起来我可能已经发现了我的问题,但我不知道为什么。当所有者被查询时
App\Opportunity::findOrFail(1)->Organization
看起来Eloquent在寻找organization_type和organization_id(小写),而不是organization_type和_id。但是,我的迁移使用$table->morphs('organization'),因此数据库中的列是用大写字母创建的。当我在数据库中将其更改为小写时,将返回结果。但不确定如何改变这种行为,它似乎是在从5.2升级后引入的
编辑:5.3中引入了一个变化,蛇的类型和id似乎是我经历的根本原因
将您的Morpto更改为基于此文档,以防止混淆laravel以检测列类型和列id
class Image extends Model
{
protected $fillable = [
"link",
"imageable_id",
"imageable_type",
];
public function imagable()
{
return $this->morphTo(__FUNCTION__, 'imageable_type', 'imageable_id');
}
}
在花了两天半的时间探索了网上找到的所有解决方案之后,我清空了缓存,一切正常
php artisan cache:clear