entity relationship - One-to-one relations that only go one way in Laravel -
is possible/right have a relation between tables only goes 1 way? have invoices
table need reference in other tables likes commission_payments
, membership_payments
invoices
table not need commission_payment_id
or membership_payment_id
. in other words, there different types of transactions can happen , may have invoice attached, invoice not need reference these transaction tables.
invoices commission_payments membership_payments --------------- --------------------- --------------------- -id -id -id ... -invoice_id -invoice_id ... ...
i have created eloquent models each table. added hasone
relation invoices
on other 2 models.
class commissionpayment extends model{ public function invoice(){ return $this->hasone('app\models\invoice'); } }
i tried accessing comission payment's attached invoice this:
$com = commissionpayment::first(); $com->invoice->id;
i error:
sqlstate[42s22]: column not found: 1054 unknown column 'invoices.commission_payment_id' in 'where clause' (sql: select * `invoices` `invoices`.`commission_payment_id` = 15 , `invoices`.`commission_payment_id` not null limit 1)
why looking commission_payment_id
field in invoices
table? expect query sort of this:
select * `invoices` `id` = 23 /* id fetched `invoice_id` field in `commission_payments` table */
do have add column each table reference invoice_id? @ moment it's 2 grow. also, when invoice generated commission payment, won't need membership payment field don't think should go there.
a one-to-one relationship setup using either hasone
or belongsto
relationship. hasone
side of relationship assumes foreign key stored on other table. belongsto
side of relationship has foreign key.
therefore, since foreign key invoices stored on commission_payments , membership_payments tables, models said belong invoices table.
change relationship hasone
belongsto
, should go:
class commissionpayment extends model{ public function invoice() { return $this->belongsto('app\models\invoice'); } }
Comments
Post a Comment