Laravel 访问和显示数据透视表数据

Laravel 访问和显示数据透视表数据,laravel,many-to-many,laravel-4,pivot-table,Laravel,Many To Many,Laravel 4,Pivot Table,理论: 用户可以参加许多活动,许多活动可以由许多用户参加。因此,我的模型中有两个多对多关系,链接到一个透视表(event_user)。在参加每个活动时,我希望能够访问数据透视表数据(event_user),以查看他们是否已经参加了活动 事件用户: --身份证 --事件id --用户id 例如,我的种子数据是: 用户1同时参加活动2和3。我希望能够在视图中显示这一点 我得到的最接近的结果是(逻辑上): 我的目标是在他们已经参加的任何活动上禁用“参加”按钮,只保留可参加的活动 任何帮助都将不胜感激。

理论:

用户可以参加许多活动,许多活动可以由许多用户参加。因此,我的模型中有两个多对多关系,链接到一个透视表(event_user)。在参加每个活动时,我希望能够访问数据透视表数据(event_user),以查看他们是否已经参加了活动

事件用户: --身份证 --事件id --用户id

例如,我的种子数据是:

用户1同时参加活动2和3。我希望能够在视图中显示这一点

我得到的最接近的结果是(逻辑上):

我的目标是在他们已经参加的任何活动上禁用“参加”按钮,只保留可参加的活动

任何帮助都将不胜感激。先谢谢你

您可能认为必要的任何附加代码:

事件模型:

<?php

namespace myApp;
use Eloquent;

class Event extends Eloquent {

    public function consultant()
    {
        return $this->belongsToMany('myApp\Consultant');
    }

    public function location()
    {
        return $this->belongsToMany('myApp\Location');
    }

    public function user()
    {
        return $this->belongsToMany('myApp\User');
    }

}
<?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->belongsToMany('Event');
    }

    public function practice()
    {
        return $this->belongToMany('Practice');
    }

我认为你走错了方向,或者我误解了

首先,您试图找到一个与当前经过身份验证的用户具有相同主键的事件,这是不正确的,尽管这很容易出错

$id = Auth::user()->id;
$attending = myApp\Event::find($id)->event; 
// equivalent of: SELECT * FROM `events` WHERE `id` = ?
相反,你会想这样做

$id = Auth::user()->id;
$attending = myApp\Event::where('user_id', $id)->get(); 
// equivalent of: SELECT * FROM `events` WHERE `user_id` = ? (user_id insted of events.id)
也就是说,只需调用auth user上的event属性就可以访问用户事件吗

$user = Auth::user();
$attending = $user->event;
为了更进一步,让您可以在foreach循环内部进行检查,您可以将上述代码升级为如下所示

$user = Auth::user();
$attending = $user->event->lists('id');
这将根据返回的事件生成一个ID数组,您需要将这些ID分配给视图

$this->layout->content = View::make('events.index', array('events' => $events, 'attending' => $attending));
现在,您可以在foreach中自由访问它

@foreach($events as $event)
<tr>
    <td>{{ $event->title }}</td>
    <td>{{ date("j F Y", strtotime($event->date)) }}</td>
    <td>{{ $event->consultant()->first()->title }} {{ $event->consultant()->first()->surname }}</td>
    <td>{{ $event->location()->first()->address_1 }}</td>
    <td>
    @if (!in_array($event->id, $attending))
        <button type="button" class="btn btn-info">Attend</button>
    @endif
    </td>
</tr>
@endforeach

最后一点,这本身不是一个问题,但在我自己的代码中,我尝试命名可能返回多个复数对象的关系,因此它将是
public function events()

这是一个非常好的解释,我非常感谢您(对于像我这样的初学者来说,这让事情变得很清楚!)名称空间对我来说是一个懒散的地方,我的坏习惯。使用$attenting=$user->event;,返回JSON中的所有内容。。包括有关用户的所有数据、哈希密码等。。。然而,有一次偶然的机会,我试图自己解决这个问题,这似乎带来了用户正在参加的两个正确事件:$attenting=myApp\Event::has('user')->get();但我现在正努力在我的观点的foreach循环中匹配这些,以交叉匹配他们是否参加!没问题,我很高兴这有帮助。
$attenting=myApp\Event::has('user')->get()的问题是指它将返回具有相应用户的事件,即使该用户不是当前用户。如果您要回显$atting的内容,那么Laravel将自动将其设置为JSON字符串,而不是使用
dd($atting)。我修改了我的答案,加入了一种检查方法。先生,你是上帝派来的,你应该为这种建议收费!非常感谢,再次感谢。不幸的是没有文档记录,为了解决您的问题,我浏览了illumb\Support\Collection的源代码。它实际上做的是从集合中生成指定属性的数组(列表)。因此,在本例中,它从集合中的每个对象中获取id属性,并生成一个很好的数组供我们使用。实际方法如下所示:
$this->layout->content = View::make('events.index', array('events' => $events, 'attending' => $attending));
@foreach($events as $event)
<tr>
    <td>{{ $event->title }}</td>
    <td>{{ date("j F Y", strtotime($event->date)) }}</td>
    <td>{{ $event->consultant()->first()->title }} {{ $event->consultant()->first()->surname }}</td>
    <td>{{ $event->location()->first()->address_1 }}</td>
    <td>
    @if (!in_array($event->id, $attending))
        <button type="button" class="btn btn-info">Attend</button>
    @endif
    </td>
</tr>
@endforeach
public function event()
{
    return $this->belongsToMany('myApp\Event');
}