Skip to content
This repository was archived by the owner on Aug 20, 2023. It is now read-only.

Commit 8ffd902

Browse files
Uploaded files
1 parent 3270933 commit 8ffd902

File tree

5 files changed

+214
-0
lines changed

5 files changed

+214
-0
lines changed

Favorite.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace CodeForms\Repositories\Favorite;
3+
4+
use Illuminate\Database\Eloquent\Model;
5+
/**
6+
* @package CodeForms\Repositories\Favorite
7+
*/
8+
class Favorite extends Model
9+
{
10+
/**
11+
* @var string
12+
*/
13+
protected $table = 'favorites';
14+
15+
/**
16+
* @var array
17+
*/
18+
protected $fillable = ['user_id'];
19+
20+
/**
21+
* @return void
22+
*/
23+
protected static function booted()
24+
{
25+
static::creating(function ($favorite) {
26+
$favorite->user_id = auth()->user()->id;
27+
});
28+
}
29+
30+
/**
31+
*
32+
*/
33+
public function favoriteable()
34+
{
35+
return $this->morphTo();
36+
}
37+
}

Favoriteable.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
namespace CodeForms\Repositories\Favorite;
3+
4+
use Illuminate\Support\Facades\DB;
5+
use Illuminate\Database\Eloquent\Builder;
6+
/**
7+
* @package CodeForms\Repositories\Favorite
8+
*/
9+
trait Favoriteable
10+
{
11+
/**
12+
* @return Illuminate\Database\Eloquent\Model
13+
*/
14+
public static function bootFavoriteable()
15+
{
16+
static::deleted(function (self $model) {
17+
$model->deleteFavorites();
18+
});
19+
}
20+
21+
/**
22+
* @example $post->hasFavorite()
23+
*
24+
* @return boolean
25+
*/
26+
public function hasFavorite(): bool
27+
{
28+
return (bool)$this->favorites()->where('user_id', auth()->user()->id)->first();
29+
}
30+
31+
/**
32+
* @example $post->addFavorite()
33+
*
34+
* @return object|boolean
35+
*/
36+
public function addFavorite()
37+
{
38+
if(!self::hasFavorite())
39+
return $this->favorites()->create();
40+
return false;
41+
}
42+
43+
/**
44+
* @example $post->removeFavorite()
45+
*
46+
* @return mixed
47+
*/
48+
public function removeFavorite(): bool
49+
{
50+
if(self::hasFavorite())
51+
return $this->favorites()->where('user_id', auth()->user()->id)->delete();
52+
return false;
53+
}
54+
55+
/**
56+
* @example $post->deleteFavorites()
57+
*
58+
* @return boolean
59+
*/
60+
public function deleteFavorites(): bool
61+
{
62+
return $this->favorites()->delete();
63+
}
64+
65+
/**
66+
* @return Illuminate\Database\Eloquent\Relations\MorphMany
67+
*/
68+
public function favorites()
69+
{
70+
return $this->morphMany(Favorite::class, 'favoriteable');
71+
}
72+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# Favoriteable
22
Simple and handy trait for Laravel based models to allow easy implementation of "favorite / remember" or "follow" feature.
3+
4+
[![GitHub license](https://img.shields.io/github/license/codeforms/Favoriteable)](https://github.com/codeforms/Favoriteable/blob/master/LICENSE)
5+
![GitHub release (latest by date)](https://img.shields.io/github/v/release/codeforms/Favoriteable)
6+
[![stable](http://badges.github.io/stability-badges/dist/stable.svg)](https://github.com/codeforms/Favoriteable/releases)

UserFavorites.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace CodeForms\Repositories\Favorite;
3+
4+
use Illuminate\Support\Facades\DB;
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Collection;
7+
/**
8+
* Trait for user model
9+
*
10+
* @package CodeForms\Repositories\Favorite
11+
*/
12+
trait UserFavorites
13+
{
14+
/**
15+
* @example $user->favorites()
16+
*
17+
* @return object|null
18+
*/
19+
public function favorites()
20+
{
21+
if(self::hasFavorite()) {
22+
$collection = new Collection;
23+
foreach($this->userFavorites()->get() as $favorite)
24+
$collection->push(app($favorite->favoriteable_type)->find($favorite->favoriteable_id));
25+
26+
return $collection;
27+
}
28+
29+
return null;
30+
}
31+
32+
/**
33+
* @example $user->hasFavorite()
34+
*
35+
* @return boolean
36+
*/
37+
public function hasFavorite(): bool
38+
{
39+
return (bool)$this->userFavorites()->count();
40+
}
41+
42+
/**
43+
* @example $user->deleteFavorites()
44+
*
45+
* @return boolean
46+
*/
47+
public function deleteFavorites(): bool
48+
{
49+
if(self::hasFavorite())
50+
return $this->userFavorites()->delete();
51+
return false;
52+
}
53+
54+
/**
55+
* @return Illuminate\Database\Eloquent\Relations\MorphMany
56+
*/
57+
public function userFavorites()
58+
{
59+
return $this->hasMany(Favorite::class, 'user_id', 'id');
60+
}
61+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateFavoritesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('favorites', function (Blueprint $table) {
17+
$table->id();
18+
$table->morphs('favoriteable');
19+
$table->unsignedInteger('user_id');
20+
$table->timestamps();
21+
22+
$table->foreign('user_id')
23+
->references('id')
24+
->on('users')
25+
->onDelete('cascade');
26+
27+
$table->unique(['favoriteable_id', 'favoriteable_type', 'user_id']);
28+
});
29+
}
30+
31+
/**
32+
* Reverse the migrations.
33+
*
34+
* @return void
35+
*/
36+
public function down()
37+
{
38+
Schema::dropIfExists('favorites');
39+
}
40+
}

0 commit comments

Comments
 (0)