tusimo / laravel-reverse-relation
为 Laravel 提供一对一和多对一反向关系
v1.0
2018-03-22 08:07 UTC
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-09-15 04:42:13 UTC
README
为 Laravel Eloquent 提供反向关系。我们定义了一对一和多对一的关系。我们经常想要获取反向关系,这意味着我们需要从数据库中进行查询。但这其实是不必要的,因为我们可能已经得到了这些数据。
安装
- 修改 composer.json
{ "require": { "tusimo/laravel-reverse-relation": "^0.1" } }
- 修改 config/app.php
<?php return [ 'providers' => [ /* * Package Service Providers... */ \Tusimo\ReverseRelation\ReverseRelationProvider::class, ] ];
使用
之前
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.
之后
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.
支持
同时也支持 tusimo/embed-relation,这是 Laravel 的一个新关系。