PHP code example of tusimo / laravel-reverse-relation

1. Go to this page and download the library: Download tusimo/laravel-reverse-relation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tusimo / laravel-reverse-relation example snippets



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.