带条件的 laravel 数据透视表

标签 laravel laravel-5 pivot-table eloquent

目前我在从数据透视表访问数据时遇到问题。

这里是我的设置,我有 3 个模型:

  • 用户
  • channel
  • 访问(作为数据中心,包含 user_id 和 channel_id)

我使用以下方法设置关系:

public function channels()
    {
        return $this->belongsToMany('App\Models\Channel', 'cms_access', 'user_id', 'channel_id')
                ->withPivot('access_analytic', 'access_live_channel', 'access_linear_channel', 'access_ads', 'access_push_notification')
                ->withTimestamps();
    }

我试图构建一个页面来显示用户的详细信息以及他/她对表的访问权限:

public function show($id)
{
    $user = User::findOrFail($id);

    // whereHas seems not working, cannot filter to specific id only
    $channels = Channel::with('users')
                        ->whereHas('users', function ($query) use($user) {
                            $query->where('users.id', '=', $user->id);
                        }) 
                        ->where('company_id', '=', $user->company_id)
                        ->where('active', '=', 1)
                        ->get();

    return view('user.show')
            ->with('user', $user)
            ->with('channels', $channels);
}

当前使用表:

<tbody>
    @forelse ($channels as $channel)
        {!! Form::open(['route' => ['access.store'], 'method' => 'POST']) !!}
        <tr>
            <td>
                <img src="{{ minio_url('channels/'.$channel->logo) }}" alt="{{ $channel->logo }}" class="img-responsive">
            </td>
            <td>{{ $channel->name_eng }}</td>
            <td>
                {!! Form::select('access_analytic', ['none' => 'None', 'read' => 'Read'], @$channel->users[0]->pivot->access_analytic, ['class' =>'form-control']) !!}
            </td>
            <td>
                {!! Form::select('access_live_channel', ['none' => 'None', 'read' => 'Read', 'read/write' => 'Read/Write', 'read/write/delete' => 'Read/Write/Delete'], @$channel->users[0]->pivot->access_live_channel, ['class' =>'form-control']) !!}
            </td>
            <td>
                {!! Form::select('access_linear_channel', ['none' => 'None', 'read' => 'Read', 'read/write' => 'Read/Write', 'read/write/delete' => 'Read/Write/Delete'], @$channel->users[0]->pivot->access_linear_channel, ['class' =>'form-control']) !!}
            </td>
            <td>
                {!! Form::select('access_push_notification', ['none' => 'None', 'read' => 'Read', 'read/write' => 'Read/Write', 'read/write/delete' => 'Read/Write/Delete'], @$channel->users[0]->pivot->access_push_notification, ['class' =>'form-control']) !!}
            </td>
            <td class="text-center">
                {!! Form::hidden('user_id', $user->id) !!}
                {!! Form::hidden('channel_id', $channel->id) !!}
                {!! Form::submit('Save', ['class' =>'btn btn-primary']) !!}
            </td>
        </tr>
        {!! Form::close() !!}
    @empty
        <tr>
            <td colspan="6" class="text-center">No channel available!</td>
        </tr>
    @endforelse
    </tbody>

我不想使用 $channel->users[0]->pivot->access_analytic 等,因为它并非一直有效 由于 whereHas 无效,有时实际用户位于用户[1],关于我应该如何访问关系的任何其他建议?

最佳答案

如果你只想让一个用户拥有每个 channel 并且只获取同一用户的 channel ,你需要结合 with()whereHas() 就像这个:

Channel::with(['users' => function($q) use($user) {
        $q->where('id', $user->id);
    }])
    ->whereHas('users', function ($q) use($user) {
        $q->where('id', $user->id);
    }) 
    ->where('company_id', $user->company_id)
    ->where('active', 1)
    ->get();

在这种情况下,只会加载一个用户。因此,您将能够通过以下方式获取数据透视数据:

$channel->users->first()->pivot->access_analytic

或者:

$channel->users[0]->pivot->access_analytic

如果您不想按用户过滤 channel ,只需删除 whereHas() 部分即可。

关于带条件的 laravel 数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48159547/

相关文章:

php - 错误日志在 laravel 5.3 中被截断

php - 在 laravel 5 中计算数据透视表中的总和

excel - 为什么我不能按名称访问工作表?

mysql - SQL/Laravel - 为什么我的查询返回空集合?

php - 使用 laravel,如何创建动态文件下载?

css - 使用 Laravel 的 elixir 导入 CSS 文件时出现 ErrorException

excel - 过滤数据透视表列,仅在以下情况下计数

python - pandas 选择数据透视表的子集

php - 不同模型类型 Laravel 上的不同关系

php - Laravel 5,按关系计数对列表进行分页和排序