Php 如何将限制设置为4个用户?

Php 如何将限制设置为4个用户?,php,database,laravel,Php,Database,Laravel,我的网站上有此部分:[1]:“部分” 我想将此照片限制为最多显示4个联系人,因为如果我有更多联系人,这看起来很糟糕。我如何才能将最大显示限制添加到此照片中?抱歉,代码太多,但我不知道我的功能具体在哪里。谢谢 我的观点如下: <!-- Tabs Widget --> <div class="tab-content" style="padding: 8px !important"> <?php $count_user = 0; ?&g

我的网站上有此部分:[1]:“部分”

我想将此照片限制为最多显示4个联系人,因为如果我有更多联系人,这看起来很糟糕。我如何才能将最大显示限制添加到此照片中?抱歉,代码太多,但我不知道我的功能具体在哪里。谢谢

我的观点如下:

<!-- Tabs Widget -->


<div class="tab-content" style="padding: 8px !important">
                <?php $count_user = 0; ?>

                @foreach($user->contact as $contact)
                    <div id="contact<?php echo $count_user++; ?>" class="tab-pane magazine-sb-categories <?php if($count_user == 1){ echo "active"; } ?>">
                        <div class="row team-v1">


                        <ul class="list-unstyled col-xs-12" style="margin-bottom: -10px">
                    <li><h3 style="margin-top: 5px !important;text-transform: none; " >
                        @if($contact->role[0]->slug == "individuals")
                                        <i style="font-size: 13px;" class="icon-user"></i>

                                    @elseif($contact->role[0]->slug =='organizations')
                                        <i style="font-size: 13px;" class="icon-hotel-restaurant-172 u-line-icon-pro fa- fa-lg"></i>
                                    @endif

                                    <a style="font-size: 14px" href="{{ url('') }}/{{ $contact->username }}">{{ $contact->username  }}</a></h3>
                                <p>
                    <strong><i class="icon-real-estate-020 u-line-icon-pro"></i> : </strong><a>{{ $contact->country->country }}</a><br>
                    <strong><i class="icon-screen-tablet fa-" aria-hidden="true"></i> : </strong><a>{{ $contact->industry->industry }}</a><br>
                    @if($contact->role[0]->slug == "individuals")
                                                    @foreach($contact->career_path as $career_path)
                                                        <i style="font-size: 13px" class="icon-speedometer"></i> : {{ $career_path->functions->function }}
                                                        @break;
                                                    @endforeach
                                                @elseif($contact->role[0]->slug =='organizations')

                                                    <i style="font-size: 13px" class="icon-frame fa-"></i> : {{ $user->organization_type->organization_type }}<br>
                                                @endif


                        </p>
                    </ul>



                        </div>
@endforeach

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

class User extends Authenticatable
{
    use SoftDeletes;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    // protected $fillable = [
    //     'name', 'email', 'password',
    // ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    // protected $hidden = [
    //     'password', 'remember_token',
    // ];

    public function comment()
    {
        return $this->hasMany('App\Comment');
    }

    public function country()
    {
        // return $this->hasOne('App\Country','state_country_id','id');
        return $this->belongsTo('App\Country','country_id','id');
    }



    public function organization_type()
    {
        // return $this->hasOne('App\Country','state_country_id','id');
        return $this->belongsTo('App\OrganizationType');
    }

    public function industry()
    {
        // return $this->hasOne('App\Country','state_country_id','id');
        return $this->belongsTo('App\Industry');
    }


    public function career_path()
    {
        return $this->hasMany('App\CareerPath');
    }


    public function education()
    {
        return $this->hasMany('App\Education');
    }

    public function about()
    {
        return $this->hasOne('App\About');
    }

    public function portfolio()
    {
        return $this->hasOne('App\Portfolio');
    }

    public function language_skills_selected()
    {
        return $this->belongsToMany('App\LanguageSkill','language_skills_selected','user_id','language_skills');
    }

     public function offices_branch()
    {
        return $this->hasMany('App\OfficesBranch');
    }

    public function my_alert()
    {
        return $this->hasOne('App\MyAlert');
    }

    public function privancy_setting()
    {
        return $this->hasOne('App\PrivancySetting');
    }

    public function event()
    {
        return $this->hasMany('App\Event');
    }

    public function news()
    {
        return $this->hasMany('App\News');
    }

    public function opinion()
    {
        return $this->hasMany('App\Opinion');
    }

    public function career_solution()
    {
        return $this->hasMany('App\CareerSolution');
    }


    public function contact()
     {
         return $this->belongsToMany('App\User','contacts','contact_id','user_id');
     }

    public function user()
    {
        return $this->belongsToMany('App\User','contacts','user_id','contact_id');
    }




}


@foreach($user->contact as$contact)

如果您为
$user->contact
(即hasMany)设置了关系,您应该能够在刀片文件的循环中这样调用它:

$limitedContacts = $user->contact->limit(4);

@foreach($limitedContacts as $contact)
     ....
@endforeach

这应该在User.php中完成。

您好!我想我们可能需要更多的背景。。。您想在从DB中提取时或之后对其进行限制吗?您指的是哪个变量?您好。我想设置从DB中提取元素时的限制。因为,就像在照片中一样,它看起来不好看。所以,我需要为显示元素添加一个限制:)我指的是这个foreach
@foreach($user->contact as$contact)
。请检查我更新的询问。类似于…limit(4)->latest()的内容;谢谢你的回答。但我有一个错误:
方法限制不存在。
。这是我的公共函数联系人,来自User.php,如果有帮助的话:
public function contact(){return$this->belongstomy('App\User','contacts','contact\id','User\id');}
谢谢你的回答,但我不知道应该把这个放在哪里。也许你能帮我一点忙?谢谢。我已经试着把它放在这里了,但并非没有效果:`$data['user']=user::with('career\u path.industry','career\u path.department','career\u path.functions','education.field\u of u study','education.degree','privacy\u setting')->where('username','=',$username)->take(2)->firstOrFail()`不,您不首先使用用户,或者在使用take()时失败。只需添加此->获取(5)->获取();或者,您也可以使用此->限制(5)->获取();代码中变量的名称是什么让我来为您编写,很抱歉,您的代码格式不好很抱歉,但这不是我的代码…我只需要修复一个错误,但我在后端做得不好:)我正在使用此变量生成用户名/图像配置文件等。
@foreach($user->contact as$contact)
public function contact() 
{ 
return $this->belongsToMany('App\User','contacts','contact_id','user_id')->limit(4); 
}