PHP code example of igaster / laravel-model-events
1. Go to this page and download the library: Download igaster/laravel-model-events 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/ */
igaster / laravel-model-events example snippets
use Igaster\ModelEvents\Traits\LogsModelEvents;
class MyModel extends Model
{
use LogsModelEvents;
class MyModel extends Model
{
public function myMethod()
{
// ...
$modelEvent = $this->logModelEvent("Something Happened!");
}
class MyModel extends Model
{
public static $logModelEvents = [
'created',
'updated',
];
// This will retrieve the last 10 events logged for $model instance.
$modelEvents = $model->getModelEvents(10);
use Igaster\ModelEvents\Traits\UserLogsModelEvents;
class User extends Authenticatable
{
use UserLogsModelEvents;
// This will retrieve the last 10 events logged by this $user.
$modelEvents = $user->getUserModelEvents(10);
$user->modelEvents; // Get all model events for $user
$model->modelEvents; // Get all model events for $model
$model->modelEvents()->where(`created_at`, '>', $yesterday)->get(); // Custom Query
// Or you can build queries with the LogModelEvent model:
LogModelEvent::whereUser($user)->whereModel($model)->get();
foreach($model->modelEvents as $modelEvent){
$modelEvent->user; // User model
$modelEvent->model; // Model related with the event (though polymorphic relathinships)
$modelEvent->description; // String
$modelEvent->created_at; // Timestamp
}