Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you're going about this the wrong way, that or I have misunderstood.</p> <p>Firstly, you're trying to find an event with the same primary key as the currently authenticated user, which isn't correct, although it's an easy hole to fall down.</p> <pre><code>$id = Auth::user()-&gt;id; $attending = myApp\Event::find($id)-&gt;event; // equivalent of: SELECT * FROM `events` WHERE `id` = ? </code></pre> <p>Instead you'll want to do this</p> <pre><code>$id = Auth::user()-&gt;id; $attending = myApp\Event::where('user_id', $id)-&gt;get(); // equivalent of: SELECT * FROM `events` WHERE `user_id` = ? (user_id insted of events.id) </code></pre> <p>That being said, surely the user events can be accessed by just calling the event property on the auth user?</p> <pre><code>$user = Auth::user(); $attending = $user-&gt;event; </code></pre> <p>To take this one step further, and make it so that you can check inside the foreach loop, you could advanced the above code to look like the following</p> <pre><code>$user = Auth::user(); $attending = $user-&gt;event-&gt;lists('id'); </code></pre> <p>This will make an array of ids from the returned events that you need to assign to the view</p> <pre><code>$this-&gt;layout-&gt;content = View::make('events.index', array('events' =&gt; $events, 'attending' =&gt; $attending)); </code></pre> <p>Now you can freely access it in your foreach</p> <pre><code>@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; @if (!in_array($event-&gt;id, $attending)) &lt;button type="button" class="btn btn-info"&gt;Attend&lt;/button&gt; @endif &lt;/td&gt; &lt;/tr&gt; @endforeach </code></pre> <p>Also, seeing as Event is a reserved Alias (unless you've modified the config, which I wouldn't recommend), you'll want to specify the namespace in the relationship declaration within User</p> <pre><code>public function event() { return $this-&gt;belongsToMany('myApp\Event'); } </code></pre> <p>As a final point, it's not an issue as such, but in my own code I try to name relationships that have the potential to return multiple objects in the plural, so it would be <code>public function events();</code>.</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.
 

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