You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+31-3Lines changed: 31 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -394,18 +394,27 @@ It is possible to have relationships between native Laravel Model objects from y
394
394
protected $connection = 'theConnectionName';
395
395
```
396
396
397
-
Once they're set correctly, you can create relationships, such as a belongsTo, by manually creating a new eloquent-filemaker belongsTo object and setting the appropriate keys.
397
+
Once the connections are set correctly, relationships from FMModel objects to sql-based Model objects should resolve correctly automatically. Relationships from regular `Model` objects to `FMModel` objects (version > 2.3.0 ) will require adding a new trait to your model class to enable the relationship to be created. For versions earlier than 2.3.0 or for more control over the relationship you can add a relationship connection manually using the examples below.
398
398
399
-
Here is an example of setting a native Laravel User Model to belong to a FileMaker-based Company FMModel class.
399
+
you can create relationships, such as a belongsTo, by manually creating a new eloquent-filemaker belongsTo object or importing a new trait and setting the appropriate keys.
400
+
401
+
### Using trait to create a relationship (2.3.0+)
402
+
The `HasHybridRelationships` trait allows the model to automatically resolve relationships from a `Model` to an `FMModel`. Here is an example of using the trait to create a native Laravel User `Model` in a SQL database to belong to a FileMaker-based Company `FMModel` class.
400
403
401
404
```php
402
405
// User.php
403
406
407
+
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Concerns\HasHybridRelationships;
408
+
404
409
class User extends Model
405
410
{
411
+
use HasHybridRelationships;
412
+
406
413
public function company()
407
414
{
408
-
return new \GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\BelongsTo(Company::query(), $this, 'company_id', 'id', '');
415
+
// The Company class is an FMModel and is stored in FileMaker
416
+
// The correct relationship will be resolved automatically thanks to the HasHybridRelationships trait
@@ -417,5 +426,24 @@ With this relationship created we can now get an FMModel of the Company the User
417
426
$company = $user->company;
418
427
```
419
428
429
+
### Manually creating a relationship
430
+
431
+
Using the `HasHybridRelationships` trait is the easiest way to create relationships between native Laravel models and FMModels. However, if you are using an older version of Eloquent FileMaker or want to manually manage the relationships you can establish the relationship by using the Eloquent FileMaker version of the relationship type. Each valid relationship type will be available under the `\GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\` namespace.
432
+
433
+
Here is an example of setting a native Laravel User Model to belong to a FileMaker-based Company FMModel class.
434
+
435
+
```php
436
+
// User.php
437
+
438
+
class User extends Model
439
+
{
440
+
public function company()
441
+
{
442
+
// The Company class is an FMModel and is stored in FileMaker
443
+
return new \GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\BelongsTo(Company::query(), $this, 'company_id', 'id', '');
444
+
}
445
+
}
446
+
```
447
+
420
448
## License
421
449
Eloquent-FileMaker is open-sourced software licensed under the MIT license.
0 commit comments