Hello, I finished the Laravel 8 from scratch course, and there's something I would like to do but I dont know if it's better to start it from zero and delete the Category entirely. My idea is that now there's a relation one to many between Post and Category, what I want is to convert that into a many to many relationship so one post can have more than 1 category, instead of just 1 category can belong to multiple posts. The problem is that since all its made (components, migrations, etc...) with the idea of just having 1 post maybe its better to delete all Category and start it from scratch.
Category Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\Category
*
*/
class Category extends Model
{
use HasFactory;
public function posts()
{
return $this->hasMany(Post::class);
}
}
Post Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\Post
*/
class Post extends Model
{
use HasFactory;
protected $with = ['category','author'];
public function scopeFilter($query, array $filters)
{
$query->when($filters['search'] ?? false, fn($query, $search) =>
$query->where(fn($query) =>
$query->where('title', 'like', '%' . $search . '%')
->orWhere('body', 'like', '%' . $search . '%')
)
);
$query->when($filters['category'] ?? false, fn($query, $category) =>
$query->whereHas('category', fn ($query) =>
$query->where('slug', $category)
)
);
$query->when($filters['author'] ?? false, fn($query, $author) =>
$query->whereHas('author', fn ($query) =>
$query->where('username', $author)
)
);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function author()
{
return $this->belongsTo(User::class, 'user_id');
}
}
What I need for sure is changing the relationship in models (maybe in the Post it should be)
return $this->hasMany(Category::class)->withPivot('category_id');
and in the Category model maybe should be
return $this->belongsToMany(Post::class)
->withPivot('post_id');
And also I would need the pivot table that I think it could be like this:
class CreatePostsCategoriesPivotTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts_categories', function (Blueprint $table) {
$table->unsignedBigInteger('post_id')->index();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->unsignedBigInteger('category_id')->index();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->primary(['category_id', 'post_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('category_post');
}
}
I would appreciate if someone can tell me if im wrong or not, or what should I just do next, because I also populated the pivot table (with random numbers of maximum posts & categories). Also at the time of serving the project I get errors with the scopeFilter (searching function) because I guess i'll need to update that to match the new categories relationship.
If someone can help me would appreciate it, if you need any other file i'll share it, but its basically the same as Laravel 8 from scratch course final files.
Thanks to everyone & Jeffrey i'm learning a lot here.