tusimo/laravel-reverse-relation
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.
安装
- 修改composer.json
{
"require":
{
"tusimo/laravel-reverse-relation": "^0.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
ort-interactive-gmbh/laravel-couchbase
A Couchbase based Eloquent model and Query builder for Laravel
348
41
spiritix/lada-cache
A Redis based, automated and scalable database caching layer for Laravel
466,118
591
jaschweder/laravel-sqlanywhere
A Sybase driver based Eloquent model and Query builder for Laravel 4
490
0