У меня есть модель бизнес-модели и подписок. Я загружать данные со следующим:Как получить данные из отношений в Laravel?
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;
}
д.Д. показывает мне следующее:
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');
}
}
Вы знаете, что вызов 'dd' остановит ваш скрипт, правильно? –
Я, если вы замените dd ($ subscription); с dd (тип $ subscription->); Ничего не возвращается. – hcker2000
Хорошо, тогда как вы определили отношения между * business * и * подпиской *? –