Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get data from a relationship in Laravel?
    primarykey
    data
    text
    <p>I have a business model and subscriptions model. I load the data with the following:</p> <pre class="lang-php prettyprint-override"><code>Business::with('subscriptions')-&gt;get() </code></pre> <p>I then created a method on my Business class as follows:</p> <pre class="lang-php prettyprint-override"><code>public function check_for_subscription($type) { if($this-&gt;subscriptions-&gt;isEmpty() === false) { foreach($this-&gt;subscriptions as $subscription) { dd($subscription); if($subscription-&gt;type == $type) { return true; } } } return false; } </code></pre> <p>The dd shows me the following:</p> <pre class="lang-none prettyprint-override"><code>object(Subscription)#175 (17) { ["connection":protected]=&gt; NULL ["table":protected]=&gt; NULL ["primaryKey":protected]=&gt; string(2) "id" ["perPage":protected]=&gt; int(15) ["incrementing"]=&gt; bool(true) ["timestamps"]=&gt; bool(true) ["attributes":protected]=&gt; array(7) { ["id"]=&gt; int(1) ["business_id"]=&gt; int(1) ["type"]=&gt; string(3) "614" ["starts_at"]=&gt; NULL ["ends_at"]=&gt; NULL ["created_at"]=&gt; string(19) "0000-00-00 00:00:00" ["updated_at"]=&gt; string(19) "0000-00-00 00:00:00" } ["original":protected]=&gt; array(7) { ["id"]=&gt; int(1) ["business_id"]=&gt; int(1) ["type"]=&gt; string(3) "614" ["starts_at"]=&gt; NULL ["ends_at"]=&gt; NULL ["created_at"]=&gt; string(19) "0000-00-00 00:00:00" ["updated_at"]=&gt; string(19) "0000-00-00 00:00:00" } ["relations":protected]=&gt; array(0) { } ["hidden":protected]=&gt; array(0) { } ["visible":protected]=&gt; array(0) { } ["fillable":protected]=&gt; array(0) { } ["guarded":protected]=&gt; array(1) { [0]=&gt; string(1) "*" } ["touches":protected]=&gt; array(0) { } ["with":protected]=&gt; array(0) { } ["exists"]=&gt; bool(true) ["softDelete":protected]=&gt; bool(false) } </code></pre> <p>If I try to do <code>$subscription-&gt;type</code> I get nothing. Any thoughts on how to get this working?</p> <p>Here is the start of my business model</p> <pre class="lang-php prettyprint-override"><code>class Business extends Eloquent { public function subscriptions() { return $this-&gt;hasMany('Subscription'); } } </code></pre> <p>Here is my subscription model</p> <pre class="lang-php prettyprint-override"><code>class Subscription extends Eloquent { public function businesses() { return $this-&gt;belongsTo('Business'); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload