我有两张表,invoice_header
和invoice_detail
两个表都有loc_id
和invo_no
作为它们之间的参考键。
如何在模型中链接它们,我可以使用数组作为外键吗?
<?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 :(得分:0)
https://laravel.com/docs/5.5/eloquent-relationships#one-to-many
您需要详细信息模型和发票标题模型以及发票模型并将它们相互连接。请按照文章或将模型重命名为发票并制作功能
//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;