我有一个商业模式和订阅模式。我使用以下内容加载数据:
Business::with('subscriptions')->get()
然后我在我的业务类上创建了一个方法,如下所示:
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;
}
dd 显示以下内容:
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');
}
}