PHP code example of lukesnowden / relationship-macros

1. Go to this page and download the library: Download lukesnowden/relationship-macros 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/ */

    

lukesnowden / relationship-macros example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Lukesnowden\RelationshipMacros\Traits\Macro;

class Customer extends Model
{

    use Macro;
    
}



namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Models\Customer;
use Some\Other\Models\Order;

class AppServiceProvider extends ServiceProvider
{

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
    
        Customer::relationshipMacro( 'orders', function() {
            return $this->hasMany( Order::class, 'customer_id' );
        });
        
    }
    
}



class MyRelationships {
    
    public function orders() : \Closure
    {
        return function() {
            return $this->hasMany( Order::class, 'customer_id' );
        };
    }
    
}

Customer::relationshipMacros( new MyRelationships );