PHP code example of jobmetric / laravel-membership

1. Go to this page and download the library: Download jobmetric/laravel-membership 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/ */

    

jobmetric / laravel-membership example snippets


namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use JobMetric\Membership\CanMember;

class User extends Authenticatable
{
    use CanMember;
}

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use JobMetric\Membership\Contracts\MemberContract;
use JobMetric\Membership\HasMember;

class Order extends Model implements MemberContract
{
    use HasMember;
    
    /**
     * allow the member collection.
     *
     * @return array
     */
    public function allowMemberCollection(): array
    {
        return [
            'owner' => 'single',
            'members' => 'multiple'
        ];
    }
}

$order = Order::find(1);
$members = $order->members();

$order = Order::find(1);
$user = User::find(1);
$order->storeMember($user, 'members');

$order = Order::find(1);
$user = User::find(1);
$order->forgetMember($user, 'members');

$order = Order::find(1);
$user = User::find(1);
$order->hasMember($user, 'members');

$order = Order::find(1);
$user = User::find(1);
$order->renewMember($user, 'members');

$order = Order::find(1);
$user = User::find(1);
$order->updateExpiredAtMember($user, 'members');

$order = Order::find(1);
$order->getPerson('members');

$user = User::find(1);
$orders = $user->persons();

$user = User::find(1);
$order = Order::find(1);
$user->storePerson($order, 'members');

$user = User::find(1);
$order = Order::find(1);
$user->forgetPerson($order, 'members');

$user = User::find(1);
$order = Order::find(1);
$user->hasPerson($order, 'members');

$user = User::find(1);
$order = Order::find(1);
$user->renewPerson($order, 'members');

$user = User::find(1);
$order = Order::find(1);
$user->updateExpiredAtPerson($order, 'members');

$user = User::find(1);
$user->getMember('members');

php artisan make:listener AddUserResourceToPersonableResourceListener

use JobMetric\Membership\Events\PersonableResourceEvent;

class AddUserResourceToPersonableResourceListener
{
    public function handle(PersonableResourceEvent $event)
    {
        $personable = $event->personable;

        if (personable instanceof \App\Models\User) {
            $event->resource = new \App\Http\Resources\UserResource($personable);
        }
    }
}

protected $listen = [
    \JobMetric\Membership\Events\PersonableResourceEvent::class => [
        \App\Listeners\AddUserResourceToPersonableResourceListener::class,
    ],
];

php artisan make:listener AddOrderResourceToMemberableResourceListener

use JobMetric\Membership\Events\MemberableResourceEvent;

class AddOrderResourceToMemberableResourceListener
{
    public function handle(MemberableResourceEvent $event)
    {
        $memberable = $event->memberable;

        if ($memberable instanceof \App\Models\User) {
            $event->resource = new \App\Http\Resources\OrderResource($memberable);
        }
    }
}

protected $listen = [
    \JobMetric\Membership\Events\MemberableResourceEvent::class => [
        \App\Listeners\AddOrderResourceToMemberableResourceListener::class,
    ],
];