Skip to content

Commit

Permalink
Merge pull request #6 from SethSharp/sharpie/dev-367-change-how-likes…
Browse files Browse the repository at this point in the history
…-work

[2.x] - Change how Like model relationships and tables work
  • Loading branch information
SethSharp authored Jun 16, 2024
2 parents c2bff9a + 9d468f4 commit 2850688
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## Upgrade Guide from 1.0 to 2.0
- Namespace for `SethSharp\BlogCrud\Models\File` has changed to `SethSharp\BlogCrud\Models\Blog`
- With the changes from [PR#5](https://github.com/SethSharp/BlogCrud/pull/5), I suggest writing a command going through all blog comments then update the comment relationship column `blog_id` to the current blog... then drop `blog_comments`
- With the changes from [PR#6](https://github.com/SethSharp/BlogCrud/pull/6), it renames the `blog_likes` table to just `likes` so relationship `likedBlogs()` on the User table is now `likes()` - and now `hasMany`. Since the relationships from the Like model has changed between the Blog and User model (to HasMany) you will need to replace any `attach` or `detach` with `create()`

## V2.0
- Moves File to proper location under `Models/Blog/` [Pull Request #3](https://github.com/SethSharp/BlogCrud/pull/3)
- Add missing size rules in requests [Pull Request #4](https://github.com/SethSharp/BlogCrud/pull/4)
- Change how Comment model relationships and tables work [Pull Request #5](https://github.com/SethSharp/BlogCrud/pull/5)
- Change how Comment model relationships and tables work [Pull Request #5](https://github.com/SethSharp/BlogCrud/pull/5)
- Change how Like model relationships and tables work [Pull Request #6](https://github.com/SethSharp/BlogCrud/pull/6)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::rename('blog_likes', 'likes');
}
};
5 changes: 2 additions & 3 deletions src/Models/Blog/Blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ public function comments(): HasMany
return $this->hasMany(config('blog-crud.models.blog.comment'));
}

public function likes(): BelongsToMany
public function likes(): HasMany
{
return $this->belongsToMany(config('blog-crud.models.iam.user'), 'blog_likes', 'blog_id', 'user_id')
->withTimestamps();
return $this->hasMany(config('blog-crud.models.blog.like'));
}

public function collection(): BelongsTo
Expand Down
2 changes: 2 additions & 0 deletions src/Models/Blog/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class Like extends Model
{
protected $guarded = [];

protected $dispatchesEvents = [
'created' => LikeCreatedEvent::class
];
Expand Down
5 changes: 2 additions & 3 deletions src/Models/Iam/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use SethSharp\BlogCrud\Database\Factories\UserFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class User extends Authenticatable
{
Expand Down Expand Up @@ -41,8 +40,8 @@ public function comments(): HasMany
->withTimestamps();
}

public function likedBlogs(): BelongsToMany
public function likes(): HasMany
{
return $this->belongsToMany(config('blog-crud.models.blog.blog'), 'blog_likes', 'user_id', 'blog_id');
return $this->hasMany(config('blog-crud.models.blog.like'));
}
}

0 comments on commit 2850688

Please sign in to comment.