2017-11-28 7 views
0

私は2つのテーブルを持っていますinvoice_headerinvoice_detail どちらのテーブルにも、参照キーとしてloc_idinvo_noがあります。Laravel | oneToManyと2つの外部キー

モデル内でそれらをリンクするにはどうすればよいですか?外部キー用に配列を使用できますか?

<?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

詳細$ details->ヘッダーでヘッダを取得できますか? – CairoCoder

+0

ええ、あなたは私が請求書モデルのために書いたものと同じことをするでしょう、そして記事は使用されているfksを指定する方法を教えます –

+0

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

関連する問題