PHP code example of imanghafoori / eloquent-relativity

1. Go to this page and download the library: Download imanghafoori/eloquent-relativity 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/ */

    

imanghafoori / eloquent-relativity example snippets


public function comments() {
    return $this->hasMany(Comment::class); 
}

class CommentsServiceProvider 
{
    public function register () {
        
        User::has_many('comments', Comment::class);     // instead of defining method on the User class.
        Article::has_many('comments',  Comment::class);
        
        Comment::belongs_to('author', User::class);       // inverse of relations
        Comment::belongs_to('article',  Article::class);
    }

}


User::find(1)->comments;
or 
User::find(1)->comments()->count();

public function comments() {
    return $this->hasMany(Comment::class); 
}

 User::has_many('comments', Comment::class);
 

User::has_many('comments', Comment::class)->orderBy('id', 'asc');

User extends Model {
    protected $with = ['comments'];
}


User::forceEagerLoading('comments');