laravel - Eloquent: Use another column as primary key on a relationship -
i have table books: id (int) , isbn (string) isbn unique key. have table store isbn book. on book model have: $this->hasmany('other', 'isbn', 'isbn');
but ignores isbn. know way of doing this?
temporary fix:
$this->primarykey = 'isbn'; return $this->hasmany('other', 'isbn');
this not possible - there few instances want link unique column on table not pk. said, other frameworks have developed methods of changing primary key, , no 1 has formally requested laravel yet. can write proposal on github , might make in future release.
looking @ specific example moment - may have unexpected outcomes when come use relationship in eager loading situations, , accounts 1 side of relationship. when add belongsto on other you'll have reverse problem.
it may possible setup relationship through isbn; take form of book hasone isbn, isbn belongsto book, isbn hasmany others, other belongsto isbn; , accessing properties $book->isbn->others
or $other->isbn->book
.
my last suggestion extend book, , set new class' primary key isbn
, base relationships on model. it's not ideal, less painful other solutions.
Comments
Post a Comment