首页 > 解决方案 > 如何将带有变量的 where 条件添加到我的控制器?

问题描述

我有一个搜索功能,我想where为结果添加一个条件。

这是我的看法

@foreach($users as $user)

            <li class="col-sm-4 col-xs-6 md-margin-bottom-30">


                <div class="team-img">
                    @if((isset($user['profile_picture'])) && !empty($user['profile_picture']))
                        @if($user['facebook_id'] == 0)

                            <?php $img = "thumbnail/".$user['profile_picture']; ?>
                            @if(@getimagesize($img))
                                <img class="img-responsive profile-img margin-bottom-20" src="{{ url('ass/263/255?'.$img) }}" alt="">
                            @else
                                <?php $img = "assets/img/team/img10-md.jpg"; ?>
                                <img class="img-responsive" src="{{ url('ass/263/255?'.$img) }}" alt="">
                            @endif
                        @else
                            <?php $img = $user['profile_picture']; ?>
                            @if(@getimagesize($img))
                                <img class="img-responsive profile-img margin-bottom-20" src="{{ url('ass/263/255?'.$img) }}" alt="" >
                            @else
                                <?php $img = "assets/img/team/img10-md.jpg"; ?>
                                <img class="img-responsive" src="{{ url('ass/263/255?'.$img) }}" alt="">
                            @endif
                        @endif
                    @else
                        <?php $img = "assets/img/team/img10-md.jpg"; ?>
                        <img class="img-responsive" src="{{ url('ass/263/255?'.$img) }}" alt="">
                    @endif


                        <ul class="social-icons social-icons-color social-cu-icons" style="display:none;">
                            <div class="social-cu">

                                <li class="facebook_share share_link" link="{{ url('') }}/{{ $user['username'] }}" name="{{ $user['username'] }}">
                                    <a href="#" data-original-title="Facebook" class="rounded social_facebook"></a>
                                </li>
                                <li class="google_share share_link" link="{{ url('') }}/{{ $user['username'] }}" name="{{ $user['username'] }}">
                                    <a href="#" data-original-title="Google Plus" class="rounded social_googleplus"></a>
                                </li>
                                <li class="twitter_share share_link" link="{{ url('') }}/{{ $user['username'] }}" name="{{ $user['username'] }}">
                                    <a href="#" data-original-title="Twitter" class="rounded social_twitter"></a>
                                </li>
                                <li class="whatapp_share share_link" link="{{ url('') }}/{{ $user['username'] }}" name="{{ $user['username'] }}">
                                    <img class="img-responsive social_whatsapp" src="{{ asset('/assets/img/icons/social/Whatsapp.png') }}" alt="">
                                </li>
                            </div>
                        </ul>


                    <ul>

                    @if(Sentinel::check())
                        @if(!in_array($user['id'], $invitation))
                            <li class="sendinvitation" value="{{ $user['id'] }}" id="sendinvitation">
                                <a href="javascript:void(0)">
                                    <i class="icon-custom icon-sm rounded-x fa fa-user-plus {{ $user['id'] }} " style="border-radius: 4px !important"></i>
                                </a>
                            </li>
                        @endif
                    @else
                        <li class="sendinvitation1" value="{{ $user['id'] }}" id="sendinvitation">
                            <a href="javascript:void(0)" onClick="return loginConf();">
                                <i class="icon-custom icon-sm rounded-x fa fa-user-plus {{ $user['id'] }} " style="border-radius: 4px !important"></i>
                            </a>
                        </li>
                    @endif

                    @if(Sentinel::check())
                        <li class="send-contact-message" data-username="{{ $user['username'] }}" data-user-id="{{ $user['id'] }}">
                            <a href="javascript:void(0)"><i class="icon-custom icon-sm rounded-x fa fa-paper-plane-o" style="border-radius: 4px !important"></i></a>
                        </li>
                    @else
                        <li class="send-contact-message1" data-user-id="{{ $user['id'] }}">
                            <a href="javascript:void(0)" onClick="return loginConf();"><i class="icon-custom icon-sm rounded-x fa fa-paper-plane-o" style="border-radius: 4px !important"></i></a>
                        </li>
                    @endif

                        <li class="share-link"><a href="javascript:void(0)" ><i class="icon-custom icon-sm rounded-x fa fa-share-alt" style="border-radius: 4px !important"></i></a></li>




                    </ul>
                </div>

                <h3 style="margin-top: 20px !important;text-transform: none"><a href="{{ url('') }}/{{ $user['username'] }}">
                    @if($user['role'][0]['slug'] == "organizations")
                            <i style="font-size: 11px" class="icon-hotel-restaurant-172 u-line-icon-pro fa- fa-lg"></i>
                            @else
                            <i style="font-size: 11px" class="icon-user"></i>
                        @endif



                        {{ $user['username'] }}  </a></h3><p>






                    <strong><i class="icon-real-estate-020 u-line-icon-pro"></i> : </strong>{{ $user['country']['country'] }} <span class="{{ $user['country']['flag'] }}"></span><br>
                    @if(!empty($user['role']))
                        @if($user['role'][0]['slug'] == "organizations")
                            <strong><i class="icon-screen-tablet fa-" aria-hidden="true"></i> : </strong>{{ $user['industry']['industry'] }}<br>
                        @endif

                        @if($user['role'][0]['slug'] == "individuals")
                            <?php $career_path = array_reverse($user['career_path']); ?>
                            @if($career_path)
                                <strong><i class="icon-screen-tablet fa-" aria-hidden="true"></i> : </strong>{{ $career_path[0]['industry']['industry'] }}<br>
                            @else
                                <strong><i class="icon-screen-tablet fa-" aria-hidden="true"></i> : </strong><br>
                            @endif
                        @endif
                    @else
                        <strong><i class="icon-screen-tablet fa-" aria-hidden="true"></i> : </strong><br>
                    @endif
                    @if(!empty($user['role']))

                        @if($user['role'][0]['slug'] == "organizations")
                            <strong><i class="icon-frame fa-" aria-hidden="true"></i> : </strong>{{ $user['organization_type']['organization_type'] }}<br>
                        @elseif($user['role'][0]['slug'] == "individuals")

                            <?php $career_path = array_reverse($user['career_path']); ?>
                            @if($career_path)
                                @foreach($career_path as $careerpath)

                                    <!-- <strong>Company:</strong>{{ $careerpath['company'] }}<br> -->
                                    <strong><i class="icon-speedometer fa-" aria-hidden="true"></i> : </strong>{{ $careerpath['functions']['function'] }}
                                 @break;
                                @endforeach
                            @else
                                <strong><i class="icon-speedometer fa-" aria-hidden="true"></i> : </strong>
                            @endif
                        @endif
                    @else
                        <strong><i class="icon-speedometer fa-" aria-hidden="true"></i> : </strong>
                    @endif
                </p>
                <br>
            </li>



        @endforeach
$users = User::with('country','industry','organization_type','career_path','career_path.industry','career_path.department','career_path.functions','role');

$private = DB::table('privacy_settings')->get();
$private = (array) $private;

$data['users'] = $users->orderBy('id', 'DESC')->skip($offset)->take($perPage)->get()->toArray();

所以,dd($private)我有一个包含多个值的数组,但我只需要 value city。在我看来,我使用foreach function, 和{{$private->city}}来显示每个用户的价值。

$data['users']现在,如果可能的话,我想在 : 中添加类似的内容where($private->city, '!=', 1)。那应该只显示带有$private->city == 0.

dd($data['users'])

array:12 [▼
  0 => array:60 [▼
    "id" => 559
    "customer_stripe_id" => null
    "username" => "Mihail22"
    "name" => "Mihail22"
    "first_name" => null
    "last_name" => null
    "dob" => "0000-00-00"
    "city" => ""
    "telephone" => ""
    "address" => ""
    "website1" => ""

这里city是城市名称

dd($私人)

array:297 [▼
  0 => {#954 ▼
    +"id": "142"
    +"user_id": "559"
    +"email": "1"
    +"website": "1"
    +"phone": "1"
    +"address": "1"
    +"city": "1"
    +"current_job": "1"
    +"job_history": "1"
    +"education_history": "1"
    +"career_solutions": "1"
    +"profile_updates": "0"
    +"contacts_list": "1"
    +"groups": "1"
  }
  1 => {#953 ▶}

city是为了隐私(1 - 公共,0 - 私人)

标签: phpdatabaselaravel

解决方案


尝试

$private = DB::table('privacy_settings')->where("city",0)->get();
print_r($private);

$data['users'] = $users->where("city",0)->orderBy('id', 'DESC')->skip($offset)->take($perPage)->get()->toArray();
print_r($data['users']);

推荐阅读