PHP code example of ricasolucoes / tecnico

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

    

ricasolucoes / tecnico example snippets


 namespace App;

use Tecnico\TecnicoGroup;

class Group extends TecnicoGroup
{
}

 namespace App;

use Tecnico\Traits\UserHasGroups;

class User extends Model {

    use UserHasGroups; // Add this trait to your model
}

    protected $routeMiddleware = [
        ...
        'groupowner' => \Tecnico\Middleware\GroupOwner::class,
        ...
    ];

Route::get('/owner', function(){
    return "Owner of current group.";
})->middleware('auth', 'groupowner');

$group    = new Group();
$group->owner_id = User::where('username', '=', 'sebastian')->first()->getKey();
$group->name = 'My awesome group';
$group->save();

$myOtherCompany = new Group();
$myOtherCompany->owner_id = User::where('username', '=', 'marcel')->first()->getKey();
$myOtherCompany->name = 'My other awesome group';
$myOtherCompany->save();

$user = User::where('username', '=', 'sebastian')->first();

// group attach alias
$user->attachGroup($group, $pivotData); // First parameter can be a Group object, array, or id

// or eloquent's original technique
$user->groups()->attach($group->id); // id only

echo "I'm currently in group: " . Auth::user()->currentGroup->name;
echo "The group owner is: " . Auth::user()->currentGroup->owner->username;

echo "I also have these groups: ";
print_r( Auth::user()->groups );

echo "I am the owner of these groups: ";
print_r( Auth::user()->ownedGroups );

echo "My group has " . Auth::user()->currentGroup->users->count() . " users.";

if( Auth::user()->isGroupOwner() )
{
    echo "I'm a group owner. Please let me pay more.";
}

$group = Auth::user()->currentGroup;
if( Auth::user()->isOwnerOfGroup( $group ) )
{
    echo "I'm a specific group owner. Please let me pay even more.";
}

try {
    Auth::user()->switchGroup( $group_id );
    // Or remove a group association at all
    Auth::user()->switchGroup( null );
} catch( UserNotInGroupException $e )
{
    // Given group is not allowed for the user
}

Tecnico::inviteToGroup( $email, $group, function( $invite )
{
    // Send email to user / let them know that they got invited
});

$user = Auth::user();

Tecnico::inviteToGroup( $user , $group, function( $invite )
{
    // Send email to user / let them know that they got invited
});

if( !Tecnico::hasPendingInvite( $request->email, $request->group) )
{
    Tecnico::inviteToGroup( $request->email, $request->group, function( $invite )
    {
                // Send email to user
    });
} else {
    // Return error - user already invited
}

$invite = Tecnico::getInviteFromAcceptToken( $request->token ); // Returns a TecnicoInvite model or null

if( $invite ) // valid token found
{
    Tecnico::acceptInvite( $invite );
}

$invite = Tecnico::getInviteFromDenyToken( $request->token ); // Returns a TecnicoInvite model or null

if( $invite ) // valid token found
{
    Tecnico::denyInvite( $invite );
}

\Tecnico\Events\UserJoinedGroup

\Tecnico\Events\UserLeftGroup

\Tecnico\Events\UserInvitedToGroup

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    ...
    \Tecnico\Events\UserJoinedGroup::class => [
        App\Listeners\YourJoinedGroupListener::class,
    ],
    \Tecnico\Events\UserLeftGroup::class => [
        App\Listeners\YourLeftGroupListener::class,
    ],
    \Tecnico\Events\UserInvitedToGroup::class => [
        App\Listeners\YourUserInvitedToGroupListener::class,
    ],
];



namespace App\Listeners;

use Tecnico\Events\UserJoinedGroup;

class YourJoinedGroupListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserJoinedGroup  $event
     * @return void
     */
    public function handle(UserJoinedGroup $event)
    {
        // $user = $event->getUser();
        // $groupId = $event->getGroupId();

        // Do something with the user and group ID.
    }
}



namespace App\Listeners;

use Tecnico\Events\UserInvitedToGroup;

class YourUserInvitedToGroupListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserInvitedToGroup  $event
     * @return void
     */
    public function handle(UserInvitedToGroup $event)
    {
        // $user = $event->getInvite()->user;
        // $groupId = $event->getGroupId();

        // Do something with the user and group ID.
    }
}

use Tecnico\Traits\UsedByGroups;

class Task extends Model
{
    use UsedByGroups;
}

// gets all tasks for the currently active group of the authenticated user
Task::all();

// gets all tasks from all groups globally
Task::allGroups()->get();
bash
php artisan vendor:publish --provider="Tecnico\TecnicoServiceProvider"
bash
php artisan migrate
bash
composer dump-autoload
bash
php artisan make:tecnico