gpt4 book ai didi

laravel - 访问和显示数据透视表数据

转载 作者:行者123 更新时间:2023-12-02 13:11:54 24 4
gpt4 key购买 nike

理论:

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

事件用户: - ID --事件ID --用户id

例如,我的种子数据是:

用户 1 参加事件 2 和 3。我希望能够在 View 中显示这一点。

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

public function index()
{
$id = Auth::user()->id;
$attending = myApp\Event::find($id)->event;
var_dump($attending); **But this var_dump returns NULL, but $id returns the correct ID.**

$events = myApp\Event::all();
$this->layout->content = View::make('events.index')->with('events', $events);
}

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

任何帮助将不胜感激。预先感谢您。

您可能认为必要的任何其他代码:

事件模型:

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

index.blade.php(显示事件列表)

<div class="panel panel-success">
<!-- Default panel contents -->
<div class="panel-heading"><strong>Events</strong></div>
<!-- <div class="panel-body">
</div> -->

<!-- Table -->
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Date</th>
<th>Presenter</th>
<th>Location</th>
<th></th>
</tr>
</thead>
<tbody>
@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><button type="button" class="btn btn-info">Attend</button></td>
</tr>
@endforeach
</tbody>
</table>
</div>

</table>

最佳答案

我认为你的处理方式是错误的,或者我误解了。

首先,您尝试查找与当前经过身份验证的用户具有相同主键的事件,这是不正确的,尽管这是一个很容易陷入困境的漏洞。

$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)

话虽这么说,用户事件肯定可以通过调用身份验证用户的事件属性来访问吗?

$user = Auth::user();
$attending = $user->event;

要更进一步,以便您可以检查 foreach 循环内部,您可以将上面的代码改进为如下所示

$user = Auth::user();
$attending = $user->event->lists('id');

这将从返回的事件中创建一个 id 数组,您需要将其分配给 View

$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

此外,由于 Event 是保留别名(除非您修改了配置,我不建议这样做),因此您需要在 User 内的关系声明中指定命名空间

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

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

关于laravel - 访问和显示数据透视表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20644001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com