PHP code example of velitsol / userstamps

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

    

velitsol / userstamps example snippets


// config/app.php

'providers' => [
    '...',
     VelitSol\Userstamps\UserstampServiceProvider::class
];

use VelitSol\Userstamps\UserstampTrait;

class Post extends Model {

    use UserstampTrait;

    protected $userstamps = [
       'created_by',
       'updated_by',
       'submitted_by',
       'deleted_by'
    ];
}

$posts = Post::withUserstamps()->get();

$post->createdByUser;
$post->updatedByUser;
$post->submittedByUser;
$post->deletedByUser;

use VelitSol\Userstamps\UserstampTrait;

class Post extends Model {

    use UserstampTrait;

    protected $userstamps = [
       // This userstamp should be set when 'creating' event is invoked.
       'created_by' => [
            'depends_on_event' => 'creating',
       ],
       // This userstamp should be set when 'creating' or 'updating' event is invoked.
       // This is an example, if a userstamp depends on multiple events
       'updated_by' => [
            'depends_on_event' => ['creating', 'updating'],
       ],
       'deleted_by' => [
             'depends_on_event' => 'deleting',
       ],

       // This userstamp should be set if "is_archived" is dirty (has some change in value)
       'archived_by' => [
            'depends_on_field' => 'is_archived'
       ],

       // This userstamp should be set if "updating" event is invoked on this model,
       // and "is_submitted" is dirty (has some change in value)
       'submitted_by'=> [
            'depends_on_event' => 'updating',
            'depends_on_field' => 'is_submitted'
       ],

       // This userstamp should be set if "updating" event is invoked on this model,
       // and provided expression evaluates to true
       'suspended_by' => [
          'depends_on_event' => 'updating',
          'depends_on_expression' => '$api_hits > 100' // $api_hits is a model field i.e $model->api_hits
       ],
       .............,
       ..............,
    ];
}

$posts = Post::withUserstamps()->get();

$post->createdByUser;
$post->updatedByUser;
$post->archivedByUser;
$post->submittedByUser;
$post->suspendedByUser;