簡體   English   中英

Laravel5.3 Eloquent Relationships沒有得到確切的記錄

[英]Laravel5.3 Eloquent Relationships not gettiing exact record

我正在使用Laravel5.3 Eloquent Relationships從三個表中獲取記錄。

客戶表:

客戶表

服務表: 在此輸入圖像描述

Subscribe_services表:

在此輸入圖像描述

我想顯示訂閱服務的客戶端列表,但我沒有獲得服務名稱我只從subscribe_service表獲取數據

客戶端型號:

    class Client extends Authenticatable
{
    use HasApiTokens, Notifiable;

    protected $table = "clients";
    protected $fillable = [
        'name','country','place','mobile_no','email', 'password','api_token',
    ];

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

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

客戶端刀片:

 <select>
                    @foreach($user->services as $services)
                    <option>{{$services->service_id}}</option>
                    @endforeach
                  </select>

我如何獲得服務名稱?請提出任何建議

如果要在此處使用hasMany() ,請執行以下操作:

$client = Client::with('services.service')->find($id);

@foreach ($client->services as $service)
    {{ $service->service->name }}
@endforeach

要使其工作,您應該將此關系添加到SubscribeService模型:

public function service()
{
    return $this->belongsTo('App\Service');
}

或者,您可以通過定義belongsToMany()關系來使用多對多關系。 在這種情況下,您可以這樣做:

$client = Client::with('services')->find($id);

@foreach ($client->services as $service)
    {{ $service->name }}
@endforeach

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM