2017 © Pedro Peláez
 

library relationships

Several Relations for Laravel.

image

resultsystems/relationships

Several Relations for Laravel.

  • Thursday, February 15, 2018
  • by emtudo
  • Repository
  • 2 Watchers
  • 7 Stars
  • 171 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 180 % Grown

The README.md

Relationships

This package adds two more kind of relationships based on Laravel's original from Has Many Through, (*1)

Installation

In the require key of composer.json file add the following:, (*2)

"resultsystems/relationships": "~0.4.0"

Important: Do not use dev-master. Instead, use the tagged version, like shown before., (*3)

Run the Composer update comand:, (*4)

composer update

or, (*5)

composer require resultsystems/relationships

, (*6)

Has One Through Several

  • Similar to Laravel's hasOne

The "has-one-through-several" relationship provides a convenient shortcut for accessing distant relations via an intermediate relation. For example, a Frequency model might have one Subject model through the intermediates Skill and Schedule model. In this example, you could easily gather subject for a given frequency. Let's look at the tables required to define this relationship:, (*7)

frequencies
    id - integer
    schedule_id - integer
    date - date

skills
    id - integer
    teacher_id - integer
    subject_id - integer
    title - string

schedules
    id - integer
    group_id - integer
    skill_id - integer
    name - string

subjects
    id - integer
    name - string

    // model = frequency
    // frequency.schedule_id = schedules.id
    // schedules.skill_id = skills.id
    // skills.subject_id = subjects.id
<?php

namespace App;

use ResultSystems\Relationships\Model;

class Frequency extends Model
{
    public function subject()
    {
        // You can add several model in array

        return $this->hasOneThroughSeveral([
            Subject::class,
            Skill::class,
            Schedule::class,
        ], null, null); // null -> optional (foreignKey -> schedule_id, localKey -> id)
    }

    // or
    public function subject()
    {
        // You can add several model in array

        return $this->hasOneThroughSeveral([
            Subject::class => [
                'subjects.id' => 'skills.subject_id',
            ],
            Skill::class => [
                'skills.id' => 'schedules.skill_id',
            ],
            Schedule::class => [
                'schedules.id' => 'frequencies.schedule_id',
            ],
        ], null, null); // null -> optional foreignKey -> schedule_id, localKey -> id)
    }
}

, (*8)

Has Many Through Several

  • Similar to Laravel's hasMany

The "has-many-through-several" relationship provides a convenient shortcut for accessing distant relations via an intermediate relation. For example, a Group model might have many Teacher models through the intermediates Schedule and Skill model. In this example, you could easily gather all teachers for a given group. Let's look at the tables required to define this relationship:, (*9)

groups
    id - integer
    name - string

teachers
    id - integer
    name - string

schedules
    id - integer
    group_id - integer
    skill_id - integer
    name - string

skills
    id - integer
    teacher_id - integer
    subject_id - integer
    title - string

    // model = group
    // groups.id = schedules.group_id
    // skills.id = schedules.skill_id
    // teachers.id = skills.teacher_id
<?php

namespace App;

use ResultSystems\Relationships\Model;

class Group extends Model
{
    public function teachers()
    {
        return $this->hasManyThroughSeveral([
           Teacher::class,
           Skill::class,
           Schedule::class => [
                'schedules.group_id' => 'groups.id',
            ],
        ]);
    }
}

, (*10)

How To Use

Mode 1

<?php

namespace App;

use ResultSystems\Relationships\Model;

class Group extends Model
{
    public function subjects()
    {
        return $this->hasManyThroughSeveral([
            Subject::class,
            Skill::class,
            Schedule::class => [
                'schedules.group_id' => 'groups.id',
            ],
        ]);
    }
}

Mode 2

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use ResultSystems\Relationships\Traits\RelationshipsTrait;

class Frequency extends Model
{
    use RelationshipsTrait;

    public function subject()
    {
        // You can add several model in array

        return $this->hasOneThroughSeveral([
            Subject::class,
            Skill::class,
            Schedule::class,
        ], null, null); // null -> option (foreignKey -> schedule_id, localKey -> id)
    }
}

The Versions

15/02 2018

dev-master

9999999-dev

Several Relations for Laravel.

  Sources   Download

MIT

The Requires

 

by Leandro Henrique Reis

laravel relation

15/02 2018

0.3.7

0.3.7.0

Several Relations for Laravel.

  Sources   Download

MIT

The Requires

 

by Leandro Henrique Reis

laravel relation

21/01 2018

dev-develop

dev-develop

Several Relations for Laravel.

  Sources   Download

MIT

The Requires

 

by Leandro Henrique Reis

laravel relation

21/01 2018

0.3.6

0.3.6.0

Several Relations for Laravel.

  Sources   Download

MIT

The Requires

 

by Leandro Henrique Reis

laravel relation