PHP code example of balfour / eloquent-dynamic-relationships

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

    

balfour / eloquent-dynamic-relationships example snippets


namespace SMSModule;

use Balfour\EloquentDynamicRelationships\HasDynamicRelationships;
use Illuminate\Database\Eloquent\Model;

class Message extends Model
{
    use HasDynamicRelationships;
    
    //
}

namespace SMSModule;

use Balfour\EloquentDynamicRelationships\Model;

class Message extends Model
{
    //   
}

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use SMSModule\Message;
use UserModule\User;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        UserModule::bond('messages', function () {
            return $this->hasMany(Message::class);
        });
    }
}

use SMSModule\Message;
use UserModule\User;

$user = User::find(1);

// retrieve a user's messages
$messages = $user->messages;

// retrieve and sort a user's messages by send date
$messages = $user->messages()
    ->latest('send_date')
    ->get();
    
// paginate a user's messages
$messages = $user->messages()
    ->paginate();

// get the user who sent the message
$message = Message::first();
$user = $message->user;

use UserModule\User;

// destroy a bond between 2 models
User::breakup('activities');

// retrieve all dynamic bonds
$bonds = User::getBonds();

// determine if a model is bonded with another
User::isBondedWith('activities');