PHP code example of lyrasoft / favorite
1. Go to this page and download the library: Download lyrasoft/favorite 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/ */
lyrasoft / favorite example snippets
$this->lang->loadAllFromVendor(\Windwalker\Language\LanguagePackage::class, 'ini');
use Lyrasoft\Favorite\Entity\Favorite;
$mapper = $orm->mapper(Favorite::class);
foreach ($faker->randomElements($items, 5) as $item) {
$favorite = new Favorite();
$favorite->setType('item');
$favorite->setUserId($user->getId());
$favorite->setTargetId($item->getId());
$mapper->createOne($favorite);
}
return [
'favorite' => [
// ...
'ajax' => [
'type_protect' => true,
'allow_types' => [
'article',
'...' // <-- Add your new types here
]
],
]
];
use Lyrasoft\Favorite\Repository\FavoriteRepository;
// In any repository
public function getFrontListSelector(?User $user = null): ListSelector
{
$selector = $this->getListSelector();
if ($user && $user->isLogin()) {
FavoriteRepository::joinFavorite(
$selector,
'item',
$user->getId(),
'item.id'
);
}
// ...
$favoriteService = $app->service(\Lyrasoft\Favorite\Service\FavoriteService::class);
$favoriteService->addFavorite($type, $user->getId(), $targetId);
$favoriteService->removeFavorite($type, $user->getId(), $targetId);
$favoriteService->isFavorited($type, $user->getId(), $targetId);
shell
php windwalker pkg:install lyrasoft/favorite -t routes -t migrations
shell
php windwalker pkg:install lyrasoft/favorite -t lang