有条件的laravel枢轴表

时间:2018-01-08 23:33:01

标签: laravel laravel-5 eloquent pivot-table laravel-eloquent

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

在这里我的设置,我有3个型号:

  • 用户
  • 频道
  • 访问(作为pivot,包含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-&gt;用户[0] - &gt; pivot-&gt; access_analytic等,因为它不会一直工作< strong>由于哪里不起作用,有时实际用户位于用户[1] ,还有关于我应该如何访问该关系的任何其他建议?

1 个答案:

答案 0 :(得分:1)

如果您只希望每个频道都有一位用户并且只获得同一用户的频道,则需要将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

如果您不想让用户过滤频道,只需删除whereHas()部分。