Laravel 4-检查用户是否使用了某些内容

哈维·祖

我正在制作一个网页,我的用户可以在其中创建两种类型的内容:“活动”和“活动集合”。我想检查当前用户是否已经在他的任何收藏夹中使用了一个活动。

这两种内容之间的关系是belongsToMany每个Activity都可以在某个Collection中使用,并且每个Collection han都有很多Activity ...因此有一个数据透视表来处理它。

我有我的Activity ID我的User ID,但我不知道该怎么做。

任何的想法??非常感谢你!!

编辑

我的控制器:

$actividads = Actividad::orderBy('created_at', 'DESC')->paginate(10);
    $id_user = Sentry::getUser()->id;
    $fichas = Ficha::where('user_id', $id_user)->orderBy('created_at', 'DESC')->get();

    $user = User::find($id_user);
    $actividadEnFicha = Actividad::has('fichas')
                                ->get();

我的看法:

@foreach($actividads as $actividad)

    @foreach($actividadEnFicha as $a)
        @if($actividad->id == $a->id)
            {{ $a->id }}
        @endif
    @endforeach
@endforeach

我想显示的名称ficha而不是$a->id(id actividad)...但我仍然不知道如何获得它。

贾里克·特卡奇克(Jarek Tkaczyk)

假设关系activitiesUser hasMany Activity):

$user = User::find($userId);

// returns Activity only if it belongs to one or more collections, null otherwise
$activityInCollection = $user->activities()
  ->has('collections')
  // with('collections') to eager load collections
  ->find($activityId);

// returns Activity with given id only if it does not belong to any collection
$activitiyWithoutCollections = $user->activities()
  ->has('collections', '=', 0);

// all activities of the user, that belong to one or more collections
$activitiesInCollections = $user->activities()
  ->has('collections')
  // with('collections') to eager load collections
  ->get();

// all activities of the user, that belong to exactly 3 collections
$activitiesInCollections = $user->activities()
  ->has('collections', '=', 3)
  // with('collections') to eager load collections
  ->get();

等等...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章