PHP code example of alyakin / favorites

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

    

alyakin / favorites example snippets


use \Alyakin\Favorites\Services\{FavoriteService, FavoriteFolderService};

$favoritesService = app(FavoriteService::class);
$folderService = app(FavoriteFolderService::class);

// Добавить объект в избранное
$favoritesService->addToFavorites($ownerId, $model);

// Добавить в конкретную папку:
$favoritesService->addToFavorites($ownerId, $model, 'read later');

// Удалить объект из избранного:
$favoritesService->removeFromFavorites($ownerId, $model);

// Создать папку
$folderService->createFolder($ownerId, 'Папка');

// Переименовать папку
$folderService->renameFolder($ownerId, $folderId, $newName);

// Переместить элемент в папку:
$favoritesService->moveToFolder($favoriteId, $folderId);

// Получить все папки владельца:
$folderService->getAllFoldersForOwner($ownerId);

// Удалить папку:
// ⚠️ ВНИМАНИЕ! При удалении папки будут также удалены все избранные элементы внутри неё.
$folderService->deleteFolder($ownerId, $folderId);

// Получить все избранные элементы владельца:
$favoritesService->getFavorites($ownerId);

// Получить избранные в конкретной папке:
$favoritesService->getFavorites($ownerId, 'funy');

// Проверить, находится ли объект в избранном:
$favoritesService->isFavorited($ownerId, $model);

class CustomFavoriteService extends \Alyakin\Favorites\Services\FavoriteService {
    // ...
}

public function addToFavorites(string $ownerId, Model $model, ?string $folderName = null): Favorite
{
    // своя логика — например, логирование
    \Log::info('Добавление в избранное', ['owner_id' => $ownerId]);

    return parent::addToFavorites($ownerId, $model, $folderName);
}

$this->app->bind(
    \Alyakin\Favorites\Services\FavoriteService::class,
    \App\Services\CustomFavoriteService::class
);