Download the PHP package shopapps/has-table-relation without Composer
On this page you can find all versions of the php package shopapps/has-table-relation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package has-table-relation
HasTable and BelongsToTable relationships for Laravel Eloquent
Provides a relation to an entire table without the need for any linked foreign key attributes.
This package provides a way to link a model to an entire table without the need for any foreign key attributes. This is useful when you have a table that contains data that should be related to a model but does not have a foreign key column to link it to.
Whilst it is always recommend to have a local_key <=> foreign_key mapping (e.g. numbers.customer_id
<=> customer.id
) for any related data which allows for the standard hasMany
/ belongsTo
relationship(s), sometimes you just inherit data and don't have the luxury of properly designed data models. :-(
NOTE: before usign this package, you could also consider using a belongsToMany
relationship and create a pivot table between the two tables. Make sure if you do that you then seed the pivot table mapping all the records you need and also keep them in sync when your code add's removes records. I have added a sample migration file at the bottom of this readme that would allow you to do this as per my customer -> numbers example scenario.
installation
HasTable relationship
Example
In this example the given Customer Model has all the records in the numbers
table related to it. Normally you would expect to see a related foreign key column such as customer_id
in the numbers
table to achieve this and use a hasMany relationship, however in my data this does not exist and due to the sensitive nature of the data I cannot add an extra column to this table.
You can now retrieve all records from the numbers table using:
BelongsToTable relationship
Example
The inverse of the hasTable relationship.
In this example all records in the numbers
table belong to the given Customer
model. Normally you would expect to see a related local key column such as customer_id
in the numbers
table to achieve this and use a belongsTo relationship, however in my data this does not exist and due to the sensitive nature of the data I cannot add an extra column to this table.
you can pass an optional second parameter to the belongsToTable method to specify the method to call on the parent model. The default is first
but you can also use last
or all
to call the corresponding method on the parent model query.
You can now retrieve the parent record from any of the the number records using:
BelongsToMany - A laravel standard relationship
Before usign this package, you could also consider using a belongsToMany
relationship and create a pivot table between the two tables. Make sure if you do that you then seed the pivot table mapping all the records you need. I have added a sample migration file below that would do this as per my customer -> numbers example scenario.