Skip to content

Instantly share code, notes, and snippets.

@anchan42
anchan42 / README.md
Created August 8, 2024 13:40 — forked from shanginn/ README.md
Get available relationships trait for Eloquent model (Laravel 5.*)

GetRelationships Trait

This trait will allow you to get defined relationships on the model. I wish Laravel have a way to get this without any additions(except for trait), but there is no way.

Here we have 2 versions of the trait: for PHP7 and for PHP5.

Both method uses Reflections to collect information about the model.

PHP7

With PHP7 version you'll only need to add return class for the relationship method like this:

let empty = {}; // An object with no properties
let point = { x: 0, y: 0 }; // Two numeric properties
let p2 = { x: point.x, y: point.y+1 }; // More complex values
let book = {
"main title": "JavaScript", // These property names include spaces,
"sub-title": "The Definitive Guide", // and hyphens, so use string literals.
for: "all audiences", // for is reserved, but no quotes.
author: { // The value of this property is
firstname: "David", // itself an object.
surname: "Flanagan"