tusimo / laravel-reverse-relation
a one to one and one to many reverse relation for laravel
Installs: 3 557
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- illuminate/container: ^5.4
- illuminate/database: ^5.4
- illuminate/events: ^5.4
- illuminate/support: ^5.4
- myclabs/deep-copy: ^1.6
This package is not auto-updated.
Last update: 2024-11-10 05:30:00 UTC
README
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.