1. Go to this page and download the library: Download wsmallnews/preference 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/ */
wsmallnews / preference example snippets
use Wsmallnews\Preference\Models;
return [
/**
* Default scopeable configuration
*/
'scopeable' => [
'scope_type' => 'sn-preference',
'scope_id' => 0,
],
/**
* Custom models
*/
'models' => [
'preference' => Models\Preference::class,
],
/**
* Base file directory, will automatically append current date (used only for filament default upload component)
*/
'file_directory' => 'sn/preference/',
];
use Illuminate\Database\Eloquent\Model;
use Wsmallnews\Preference\Models\Concerns\Preferenceable;
use Wsmallnews\Preference\Models\Concerns\Preferencer;
use Wsmallnews\Preference\Models\Concerns\Preferenceable\Likeable;
use Wsmallnews\Preference\Models\Concerns\Preferenceable\Followable;
use Wsmallnews\Preference\Models\Concerns\Preferenceable\Viewable;
use Wsmallnews\Preference\Models\Concerns\Preferencer\Liker;
use Wsmallnews\Preference\Models\Concerns\Preferencer\Follower;
use Wsmallnews\Preference\Models\Concerns\Preferencer\Viewer;
class Post extends Model
{
use Preferenceable; // Basic preference capability
use Likeable; // Can be liked
use Followable; // Can be followed
use Viewable; // Can be viewed
}
class User extends Model
{
use Preferencer; // Basic operator capability
use Liker; // Can perform like operations
use Follower; // Can perform follow operations
use Viewer; // Can perform view operations
}
$post = Post::find(1);
$user = User::find(1);
// Like
$user->like($post);
// Unlike
$user->unlike($post);
// Toggle like status
$user->toggleLike($post);
// Check if liked
$user->hasLiked($post); // true / false
// Batch attach like status
$user->attachLikeStatus($posts);
// Check if post is liked by user (from Likeable side)
$post->isLikedBy($user); // true / false
counter: {"like_num": 1}
$userA = User::find(1);
$userB = User::find(2);
// Follow
$userA->follow($userB);
// Unfollow
$userA->unfollow($userB);
// Toggle follow status
$userA->toggleFollow($userB);
// Check if following
$userA->isFollowing($userB); // true / false
// Check if mutual follow
$userA->isMutualFollowed($userB); // true / false
// Get following count
$userA->followingCount(); // Number of following
// Batch attach follow status
$userA->attachFollowStatus($userB);
// Bidirectional counts automatically update after following
counter: {"follow_num": 1, "followed_num": 1}
// When B also follows A, the followed_at timestamp is automatically written to options
options: {"followed_at": "2023-01-01 00:00:00"}
$post = Post::find(1);
$user = User::find(1);
// User views post
$user->view($post);
// Anonymous view (only increments counter, no viewer recorded)
$post->view(); // Automatically called from Viewable side
// Check if viewed
$user->hasViewed($post); // true / false
// Check if post is viewed by user (from Viewable side)
$post->isViewedBy($user); // true / false
// Delete view record
$user->deleteView($post);
// Batch attach view status
$user->attachViewStatus($posts);
// Clear all views of a type
$user->clearAllViews(Post::class);
// Clear views within a scope
$user->clearScopeableViews(
['scope_type' => 'blog', 'scope_id' => 1],
Post::class
);