2017-11-04 142 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

感谢它的工作。干杯 –

相关问题