laravel 枢轴 table 有条件
laravel pivot table with condition
目前我在从 pivot table 访问数据时遇到问题。
这是我的设置,我有 3 个模型:
- 用户
- 频道
- 访问(作为枢轴,包含 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();
}
我尝试构建一个页面来显示具有 his/her 访问权限的用户的详细信息 table:
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);
}
目前正在使用 table:
<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, etc 因为它不是所有的times 由于 whereHas 不起作用,有时实际用户位于 users[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()
部分。
目前我在从 pivot table 访问数据时遇到问题。
这是我的设置,我有 3 个模型:
- 用户
- 频道
- 访问(作为枢轴,包含 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();
}
我尝试构建一个页面来显示具有 his/her 访问权限的用户的详细信息 table:
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);
}
目前正在使用 table:
<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, etc 因为它不是所有的times 由于 whereHas 不起作用,有时实际用户位于 users[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()
部分。