Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I just found something in the user guide, apparently you need the <a href="http://laravel.com/api/class-Laravel.Database.Eloquent.Relationships.Has_Many_And_Belongs_To.html#_with"><code>with()</code></a> method.</p> <p>From the <a href="http://laravel.com/docs/database/eloquent#many-to-many">User-Guide</a>:</p> <blockquote> <p>By default only certain fields from the pivot table will be returned (the two id fields, and the timestamps). If your pivot table contains additional columns, you can fetch them too by using the with() method :</p> <pre class="lang-php prettyprint-override"><code>class User extends Eloquent { public function roles() { return $this-&gt;has_many_and_belongs_to('Role', 'user_roles')-&gt;with('column'); } } </code></pre> </blockquote> <p>So you can then use something similar to this when defining your relationship:</p> <pre class="lang-php prettyprint-override"><code>$this-&gt;has_many_and_belongs_to('User')-&gt;with('playcount'); </code></pre> <h2>Example</h2> <p>I just used this to make sure it works...</p> <pre class="lang-php prettyprint-override"><code>class Song extends Eloquent { function users() { return $this-&gt;has_many_and_belongs_to('User')-&gt;with('playcount'); } } class User extends Eloquent { function songs() { return $this-&gt;has_many_and_belongs_to('Song')-&gt;with('playcount'); } } // My test method class TestOrm extends PHPUnit_Framework_TestCase { public function testSomethingIsTrue() { foreach(User::find(3)-&gt;songs()-&gt;order_by('playcount')-&gt;get() as $song) echo $song-&gt;name, ': ', $song-&gt;pivot-&gt;playcount, "\n"; echo "\n"; foreach(User::find(3)-&gt;songs()-&gt;order_by('playcount','desc')-&gt;get() as $song) echo $song-&gt;name, ': ', $song-&gt;pivot-&gt;playcount, "\n"; } } </code></pre> <h2>Output</h2> <pre><code>Jingle Bells: 5 Mary had a little lamb: 10 Soft Kitty: 20 The Catalyst: 100 The Catalyst: 100 Soft Kitty: 20 Mary had a little lamb: 10 Jingle Bells: 5 </code></pre> <p><strong>Note</strong>: It is no coincidence that without using <code>order_by()</code> the result appears sorted in <code>ascending</code> order by the <code>playcount</code>. I confirmed this through testing (as I do not know yet how to display queries in unit tests), but you should probably not rely on this behaviour.</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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