| title | description | keywords | tags | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Customizing User Interaction | Laravel Like |
Learn how to customize user interactions in the Laravel Like package, including custom interaction models, custom user models, and extending default behavior. |
|
|
This guide explains how to customize user interactions in the Laravel Like package. You can extend the default behavior by creating custom interaction models, configuring user models, and adding your own interaction logic.
- Laravel 9.0 or higher
- PHP 8.1 or higher
- Laravel Like package installed and configured
- Basic understanding of User Interaction Trait
If your application uses a custom user model or a different primary key, update the config/like.php file:
// config/like.php
return [
'interaction_model' => \CSlant\LaravelLike\Models\Like::class,
'users' => [
'model' => \App\Models\CustomUser::class, // Your custom user model
'foreign_key' => 'author_id', // Custom foreign key
],
];You can create a custom interaction model to extend the default Like model with additional fields or methods.
namespace App\Models;
use CSlant\LaravelLike\Models\Like;
class Interaction extends Like
{
/**
* Additional fillable attributes.
*/
protected $fillable = [
'user_id',
'model_id',
'model_type',
'type',
'comment', // Custom field
'metadata', // Custom field
];
/**
* Custom cast attributes.
*/
protected $casts = [
'model_type' => 'string',
'type' => \CSlant\LaravelLike\Enums\InteractionTypeEnum::class,
'metadata' => 'array',
];
/**
* Get a summary of the interaction.
*/
public function getSummaryAttribute(): string
{
return "{$this->user->name} {$this->type->value}d this content";
}
}use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('likes', function (Blueprint $table) {
$table->text('comment')->nullable()->after('type');
$table->json('metadata')->nullable()->after('comment');
});
}
public function down(): void
{
Schema::table('likes', function (Blueprint $table) {
$table->dropColumn(['comment', 'metadata']);
});
}
};// config/like.php
return [
'interaction_model' => \App\Models\Interaction::class,
// ...
];You can override methods from the UserHasInteraction trait in your User model:
use CSlant\LaravelLike\UserHasInteraction;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use UserHasInteraction;
/**
* Get only liked content by this user.
*/
public function likedContent()
{
return $this->likes()->where('type', 'like');
}
/**
* Get only disliked content by this user.
*/
public function dislikedContent()
{
return $this->likes()->where('type', 'dislike');
}
/**
* Get only loved content by this user.
*/
public function lovedContent()
{
return $this->likes()->where('type', 'love');
}
/**
* Get interactions for a specific model type.
*/
public function interactionsFor(string $modelClass)
{
return $this->likes()->where('model_type', $modelClass);
}
}$user = User::find(1);
// Get all liked posts
$likedPosts = $user->likedContent()
->where('model_type', Post::class)
->with('model')
->get();
// Get all interactions for articles
$articleInteractions = $user->interactionsFor(Article::class)->get();The package uses the InteractionTypeEnum enum with built-in types: like, dislike, and love. If you want to add custom logic based on these types:
use CSlant\LaravelLike\Enums\InteractionTypeEnum;
class User extends Authenticatable
{
use UserHasInteraction;
/**
* Get the user's interaction stats.
*/
public function getInteractionStats(): array
{
$interactions = $this->likes()->get();
return [
'likes' => $interactions->where('type', InteractionTypeEnum::LIKE)->count(),
'dislikes' => $interactions->where('type', InteractionTypeEnum::DISLIKE)->count(),
'loves' => $interactions->where('type', InteractionTypeEnum::LOVE)->count(),
'total' => $interactions->count(),
];
}
/**
* Get the user's most interacted content type.
*/
public function getMostInteractedType(): ?string
{
return $this->likes()
->selectRaw('model_type, COUNT(*) as count')
->groupBy('model_type')
->orderByDesc('count')
->value('model_type');
}
}// In your controller
public function dashboard()
{
$user = auth()->user();
$data = [
'stats' => $user->getInteractionStats(),
'recent_likes' => $user->likedContent()
->with('model')
->latest()
->take(5)
->get(),
'recent_loves' => $user->lovedContent()
->with('model')
->latest()
->take(5)
->get(),
];
return view('dashboard', $data);
}- Learn about User Interaction Trait for basic setup
- Check out Change Default Interaction model
- Explore Query Scopes for advanced queries