A toolkit package for social networks
- Run the command below to add this package:
composer require miladimos/laravel-social
- Open your config/socials.php and add the following to the providers array:
Miladimos\Social\Providers\SocialServiceProvider::class,
- Run the command below to install package:
php artisan social:install
- Run the command below to migrate database:
php artisan migrate
First add Taggable
trait to models that you want have tags
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Miladimos\Social\Traits\Taggable;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory,
Taggable;
}
Second you can work with tags:
namespace App\Http\Controller;
use App\Models\Post;
use Miladimos\Social\Models\Tag;
class YourController extends Controller
{
public function index()
{
// first you can create custom tags
$tag = Tag::create(['name' => 'tag']);
$post = Post::first();
$post->tags; // return attached tags
$post->attach($tag); // attach one tag
$post->detach($tag); // detach one tag
$post->syncTags([$tags]); // sync tags
$tag->taggables; // return morph relation to tagged model
}
}
tag model have soft deletes trait.
First add Taggable
trait to models that you want have attachments
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Miladimos\Social\Traits\Taggable;
class Post extends Model
{
use HasFactory,
Taggable;
}
in controllers you have these methods:
namespace App\Http\Controllers;
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
$post = Post::find(1);
$post->likes // return all likes
}
}
Like
Favorite
Bookmark
Follow \ Unfollow
Comment
$post = Post::find(1);
$post->comment('This is a comment');
$post->commentAsUser($user, 'This is a comment from someone else'); $comment = $post->comments->first();
$comment->approve();
Auto Approve Comments implements Commentator needsCommentApproval false
// Retrieve all comments $comments = $post->comments;
// Retrieve only approved comments $approved = $post->comments()->approved()->get();
Vote / Rate System