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.
With PHP7 version you'll only need to add return class for the relationship method like this:
//...
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Shanginn\Crudroller\Eloquent\Concerns\GetRelationships; // Don't forget to change namespace
class ExampleModel extends Model
{
use GetRelationships;
public function exampleRelation() : belongsTo
{
return $this->belongsTo(AnotherModer::class);
}
//...
That's it!
##PHP5
With PHP5 you need to add docBlock comment with the @Relation tag.
//...
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Shanginn\Crudroller\Eloquent\Concerns\GetRelationships; // Don't forget to change namespace
class ExampleModel extends Model
{
use GetRelationships;
/**
* Example relation
*
* @return BelongsTo
* @Relation
*/
public function exampleRelation()
{
return $this->belongsTo(AnotherModer::class);
}
//...
Return format is a little different for the two:
PHP7-version will return associative array with keys set to relationships functions names and values set to classes ot those relationships.
Ex.
[
"category" => "Illuminate\Database\Eloquent\Relations\BelongsTo",
"currency" => "Illuminate\Database\Eloquent\Relations\HasMany"
]
PHP5-version will return only relationships functions names, because I'm using PHP7 and too lazy to write regular expression to parse this value from the docBlock.
Ex.
[
0 => "category",
1 => "currency"
]