Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing and displaying pivot table data
    primarykey
    data
    text
    <p>Theory:</p> <p>Users can attend many events and many events can be attended by many users. Therefore, I have two many-to-many relationships within my models, linking to a pivot table (event_user). On attending each event, I want to be able to access the pivot table data (event_user) to see if they're already attending.</p> <p>event_user: --id --event_id --user_id</p> <p>For example, my seed data is:</p> <p>user 1 attending both event 2 and 3. I want to be able to show this within a view.</p> <p>The closest I have got is (logically):</p> <pre><code>public function index() { $id = Auth::user()-&gt;id; $attending = myApp\Event::find($id)-&gt;event; var_dump($attending); **But this var_dump returns NULL, but $id returns the correct ID.** $events = myApp\Event::all(); $this-&gt;layout-&gt;content = View::make('events.index')-&gt;with('events', $events); } </code></pre> <p><strong>My aim is to disable the 'attend' button, on any event where they are already attending, only leaving the attend-able events available!</strong></p> <p><em>Any help would be hugely appreciated. Thank you in advance.</em></p> <p>Any additional code which you may find necessary:</p> <p><strong>Events Model:</strong></p> <pre><code>&lt;?php namespace myApp; use Eloquent; class Event extends Eloquent { public function consultant() { return $this-&gt;belongsToMany('myApp\Consultant'); } public function location() { return $this-&gt;belongsToMany('myApp\Location'); } public function user() { return $this-&gt;belongsToMany('myApp\User'); } } </code></pre> <p><strong>User Model:</strong></p> <pre><code>&lt;?php namespace myApp; use Eloquent; use Illuminate\Auth\UserInterface; use Illuminate\Auth\Reminders\RemindableInterface; class User extends Eloquent implements UserInterface, RemindableInterface { public function event() { return $this-&gt;belongsToMany('Event'); } public function practice() { return $this-&gt;belongToMany('Practice'); } </code></pre> <p><strong>index.blade.php (showing the event list)</strong></p> <pre><code>&lt;div class="panel panel-success"&gt; &lt;!-- Default panel contents --&gt; &lt;div class="panel-heading"&gt;&lt;strong&gt;Events&lt;/strong&gt;&lt;/div&gt; &lt;!-- &lt;div class="panel-body"&gt; &lt;/div&gt; --&gt; &lt;!-- Table --&gt; &lt;table class="table"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Title&lt;/th&gt; &lt;th&gt;Date&lt;/th&gt; &lt;th&gt;Presenter&lt;/th&gt; &lt;th&gt;Location&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; @foreach($events as $event) &lt;tr&gt; &lt;td&gt;{{ $event-&gt;title }}&lt;/td&gt; &lt;td&gt;{{ date("j F Y", strtotime($event-&gt;date)) }}&lt;/td&gt; &lt;td&gt;{{ $event-&gt;consultant()-&gt;first()-&gt;title }} {{ $event-&gt;consultant()-&gt;first()-&gt;surname }}&lt;/td&gt; &lt;td&gt;{{ $event-&gt;location()-&gt;first()-&gt;address_1 }}&lt;/td&gt; &lt;td&gt;&lt;button type="button" class="btn btn-info"&gt;Attend&lt;/button&gt;&lt;/td&gt; &lt;/tr&gt; @endforeach &lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; &lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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