Giter Site home page Giter Site logo

relationships's Introduction

Relationships

This package adds two more kind of relationships based on Laravel's original from Has Many Through

Installation

In the require key of composer.json file add the following:

"resultsystems/relationships": "~0.4.0"

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

Run the Composer update comand:

composer update

or

composer require resultsystems/relationships

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:

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)
    }
}

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:

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',
            ],
        ]);
    }
}

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)
    }
}

relationships's People

Contributors

dylan-dpc avatar emtudo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.