2017-11-28 2 views
0

두 개의 테이블 invoice_headerinvoice_detail 두 테이블의 참조 키는 loc_idinvo_no입니다.Laravel | oneToMany 두 개의 외래 키

모델 내부에서 어떻게 연결할 수 있습니까? 외래 키 배열을 사용할 수 있습니까?

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class InvoiceDetail extends Model 
{ 
    protected $table = 'invoice_detail'; 

    public $timestamps = false; 

    protected $fillable = ['invo_no','loc_id','serial','item_id','qty','rtp','cost', 
          'discount','type']; 

    public function item(){ 
     return $this->belongsTo('App\InvoiceHeader', ['loc_id', 'invo_no']); 
    } 
} 

답변

0

https://laravel.com/docs/5.5/eloquent-relationships#one-to-many

당신은 세부 모델 및 송장 헤더 모델 및 송장 모델을 원할 것입니다 간 them.follow 문서를 연결하거나 청구 할 모델의 이름을 변경하고 기능을 할

//details 
public function invoice(){ 
    return $this->belongsTo(Invoice::class); 

} 
//invoice model 
public function details(){ 
    return $this->hasMany(InvoiceDetails::class); 

} 
public function headers(){ 
    return $this->hasMany(InvoiceHeaders::class); 

} 
//headers model 
public function invoice(){ 
    return $this->belongsTo(Invoice::class); 

} 



//then you can get details and headers through 

$invoice = Invoice::find(1); 
$details = $invoice->details; 
$headers = $invoice->headers; 
+0

세부 사항 $ 세부 정보 -> 헤더를 통해 헤더를받을 수 있습니까? – CairoCoder

+0

네, 내가 인보이스 모델에 대해 쓴 것과 똑같은 일을 할 것이고, 사용중인 fks를 지정하는 방법을 알려주는 문서가 있습니다. –

+0

'return $ this-> hasMany ('App \ Comment', 'foreign_key', 'local_key') ; ' –