我有一个商业模式和订阅模式。我以下列方式加载数据:
Business::with('subscriptions')->get()然后,我在Business上创建了一个方法,如下所示:
public function check_for_subscription($type)
{
if($this->subscriptions->isEmpty() === false)
{
foreach($this->subscriptions as $subscription)
{
dd($subscription);
if($subscription->type == $type)
{
return true;
}
}
}
return false;
}国防部向我展示了以下内容:
object(Subscription)#175 (17) {
["connection":protected]=>
NULL
["table":protected]=>
NULL
["primaryKey":protected]=>
string(2) "id"
["perPage":protected]=>
int(15)
["incrementing"]=>
bool(true)
["timestamps"]=>
bool(true)
["attributes":protected]=>
array(7) {
["id"]=>
int(1)
["business_id"]=>
int(1)
["type"]=>
string(3) "614"
["starts_at"]=>
NULL
["ends_at"]=>
NULL
["created_at"]=>
string(19) "0000-00-00 00:00:00"
["updated_at"]=>
string(19) "0000-00-00 00:00:00"
}
["original":protected]=>
array(7) {
["id"]=>
int(1)
["business_id"]=>
int(1)
["type"]=>
string(3) "614"
["starts_at"]=>
NULL
["ends_at"]=>
NULL
["created_at"]=>
string(19) "0000-00-00 00:00:00"
["updated_at"]=>
string(19) "0000-00-00 00:00:00"
}
["relations":protected]=>
array(0) {
}
["hidden":protected]=>
array(0) {
}
["visible":protected]=>
array(0) {
}
["fillable":protected]=>
array(0) {
}
["guarded":protected]=>
array(1) {
[0]=>
string(1) "*"
}
["touches":protected]=>
array(0) {
}
["with":protected]=>
array(0) {
}
["exists"]=>
bool(true)
["softDelete":protected]=>
bool(false)
}如果我试着做$subscription->type,我什么也得不到。对如何让这件事奏效有什么想法吗?
这是我的商业模式的开始
class Business extends Eloquent
{
public function subscriptions()
{
return $this->hasMany('Subscription');
}
}这是我的订阅模式
class Subscription extends Eloquent
{
public function businesses()
{
return $this->belongsTo('Business');
}
}发布于 2013-11-12 18:18:24
根据dd()输出,订阅对象没有名为"type“的属性。这就解释了为什么你没有从$订阅->类型得到任何东西。
根据dd()输出,订阅对象do有一个名为"attributes“的受保护属性,它是一个数组。数组的一个键是"type“,所以我假设这就是您要达到的值。
由于“属性”数组是受保护的,所以不能从外部类访问它。我假设您的订阅类有一个名为getAttributes()的getter函数,它返回受保护的数组。如果是这样..。你唯一需要的就是:
public function check_for_subscription($type)
{
if($this->subscriptions->isEmpty() === false)
{
foreach($this->subscriptions as $subscription)
{
$attributes = $this->subscriptions->getAttributes();
if($attributes['type'] == $type)
{
return true;
}
}
}
return false;
}https://stackoverflow.com/questions/18236209
复制相似问题