New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

tusimo/laravel-reverse-relation

a one to one and one to many reverse relation for laravel
4,777 1
Install
composer require tusimo/laravel-reverse-relation
Latest Version:v1.0
Laravel:5
License:MIT
Last Updated:Mar 23, 2018
Links: GitHub  ·  Packagist
Maintainer: tusimo

LaravelReverseRelation

Reverse relation for laravel eloquent. We define one to one and one to many relations. We often want to get the reverse relation which means we should query from database. And this is unnecessary.Because we maybe already get the data.

安装

  1. 修改composer.json
{
    "require":
    {
        "tusimo/laravel-reverse-relation": "^0.1"
    }
}
  1. 修改config/app.php
<?php
return [
    'providers' => [
        /*
         * Package Service Providers...
         */
        \Tusimo\ReverseRelation\ReverseRelationProvider::class,
    ]
];

使用

before:

class User extends Model {
    use \Tusimo\ReverseRelation\Traits\ReverseRelation;

    public function books ()
    {
        return $this->hasMany(Book::class);
    }
}

class Book extends Model {
    public function user ()
    {
        return $this->>belongsTo(User::class);
    }
}
$books = User::with('books')->first();
dd($books->first()->user);//we maybe use like this way.this will be a sql query for db.

after:

class User extends Model {
    use \Tusimo\ReverseRelation\Traits\ReverseRelation;

    public function books ()
    {
        return $this->hasMany(Book::class)->withReverse('user');
    }
}

class Book extends Model {
    public function user ()
    {
        return $this->>belongsTo(User::class);
    }
}
$books = User::with('books')->first();
dd($books->first()->user);//this time there will be no sql for db because we have already know.

support

also support for tusimo/embed-relation which is a new relation for laravel.

Related Packages

laragear/meta-model

Let other developers customize your package model and migrations

2,003,720 1
tusimo/embed-relation

a relation for laravel

10,959 3
pdphilip/opensearch

An OpenSearch implementation of Laravel's Eloquent ORM

480,696 0
designmynight/laravel-elasticsearch

Use Elasticsearch as a database in Laravel to retrieve Eloquent models and perfo...

45,415 30
friendsofcat/laravel-couchbase

A Couchbase based Eloquent model and Query builder for Laravel

31,062 14

Version History

Version Released Laravel License
v1.0 ^5.4 MIT
v0.1.2 ^5.4 MIT
v0.1.1 ^5.4 MIT
v0.1 ^5.4 MIT