如何仅查询表Laravel中的1级和2级用户列表

时间:2018-07-05 13:55:59

标签: laravel

我创建了引用链接寄存器系统 m。之后,我需要制作视图刀片表格以显示仅来自级别1和2的用户列表。 因为我想将此用户归功于推介系统。

1级奖励-% 2级加成-% 等等

谢谢

此处是代码

usercontroller

public function referral() {
  $user = User::find(1);

  return view('users.referral', compact('user'));
}

public function viewreferral() {
    $users = User::where('id', '!=', Auth::id())->get();
    return view('users.viewreferral',compact('users'));
  }

[用户表]

https://i.stack.imgur.com/BAogM.png

我在视图中使用它,但不起作用

<table class="table table-bordered">
                  <thead>
                  <th>Level</th>
                  <th>UserName</th>
                  <th>Email</th>
                  <th>Referred_by</th>
                  <th>Affiliate_id</th>
                  <th>Activation</th>
                  </thead>
             <tbody>

              @foreach($users as $singleUser)

                   <tr>
                  @if($singleUser->level === 1 || $singleUser->level === 2)
                    <td>{{$singleUser->id}}</td>
                    <td>{{$singleUser->name}}</td>
                    <td>{{$singleUser->email}}</td>
                    <td>{{$singleUser->referred_by}}</td>
                    <td>{{$singleUser->affiliate_id}}</td>
                    <td><a href="{{'/activate'}}">Activation</td>
                 @endif
                  </tr>

              @endforeach
            </tbody>

这里是myregistercontroller

protected function create(array $data)
{
       $referred_by = Cookie::get('referral');

            $user =  User::create([
                      'name' => $data['name'],
                      'email' => $data['email'],
                      'password' => Hash::make($data['password']),
                      'affiliate_id' => str_random(10),
                      'referred_by'   => $referred_by,
                        ]);

                   return $user;
  }

2 个答案:

答案 0 :(得分:1)

也许是这样的:

@foreach($user as $singleUser)
 @if($singleUser->level === 1 || $singleUser->level === 2)
    {{ $singleUser->name }}
 @endif
@endforeach

答案 1 :(得分:0)

在用户迁移过程中,添加类似user_level的内容。那么您可以像

那样遍历用户
@foreach($users as $user)
    @if($user->level === 1)
        //do something
    @elseif($user->level === 2)
        //do something else
    @else
        //user does not have level
    @endif
@endforeach