2017-11-04 138 views
0

在橫幅表中,我有另一個字段image_id,它也指媒體庫,我需要知道如何定義此橫幅模型,因爲我已經已經定義了video_id belongsTo mediagallery。我需要此幫助需要知道如何定義在Laravel模型中引用同一父表的兩個字段

//********** Banner model *************************** 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Banner extends Model 
{ 
    // 

    protected $table='banners'; 

    public function mediagallery() 
    { 

     return $this->belongsTo('App\Mediagallery', 'video_id','id'); // 2nd foreign key field name , 3td is parent table primary key field name 
    } 
} 

//*********** Mediagallery model ********************* 

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Mediagallery extends Model 
{ 
    // 

    protected $table='mediagalleries'; 

    public function banner() 
    { 

     return $this->hasOne('App\Banner', 'video_id', 'id'); // 2nd foreign key o the child table , 3td is parent or local table 
    } 


} 

回答

0

創建2個關係。一種關係爲視頻而另一種關係爲圖像。

public function videoGallery() 
{ 
    return $this->belongsTo('App\Mediagallery', 'video_id','id'); 
} 

public function imageGallery() 
{ 
    return $this->belongsTo('App\Mediagallery', 'image_id','id'); 
} 

我不知道應用程序的具體細節,希望這有助於。

+0

感謝它的工作。乾杯 –

相關問題