I have a Post model and Post model can have many Categories, also Category has many SubCategories.
So the use can assign only Categories under a Post, also if the want they can assign SubCategories based on the Categories they already selected.
What should be my database setup?
Right now what I am thinking is.
posts
categories
subCategories
post_category // saves the categories for each post
post_subcategory // saves the subCategories for each post
first of all, you may create only one Category table which includes id and parent_id, and in the Category model;
Create a method that is about having subCategories with hasMany(),
And then also, create a method that has parentCategories with belongsTo().
So that you may call each of them from your controllers by using these methods.
Hope that this will help you to handle your problem.
Related
I have users in my laravel site, each user has several posts, each posts has several category, each category has several subcategories. How should be database and php structure. Currently I have users table with id, name....and posts table with id, user_id .....I have some belongstomany function. But I don't know this much depth/inner go, like users>posts>category>subcategory. Please give outline. Thanks.
I have categories and product table. i want to show the categories which has at least 1 product
First you have to make one to many relationship it`s described there: https://laravel.com/docs/8.x/eloquent-relationships#one-to-many
And then you can call and check if it has products
Use has.
$categories = Category::has('products')->get();
This will return all categories that have at least one product.
I'm using laravel 5.6 and i have two tables categories and posts
I have created category_id in posts table which looks like
{id, category_id, title, description, created_at, updated_at}
I created a drop-down on the post create and edit form to select the category which works fine.
Now I am looking for something more advanced where a post can have multiple categories. I have changed belongTo to HasMany categories in post model.
I feel I am doing it the wrong way. Do i need to create another table i.e.,
post_categories
{id, category_id, post_id}
The reason i want to do this is because i have multiple posts which belong to multiple categories and my route is like this
site.com/categoryname/post-slug
So few posts appear in multiple categories.
You probably will need to use the pivot table. That way you can data mine. Even if you don't have a multi-select, you'll easily be able to collect posts linked to categories and vice versa. laraveldaily-good example. They use the sync method, one of my favs. when you save the form data you can just do something like App\Post::find($id)->categories()->sync(request()->input(categories')) and laravel will handle the rest for you.
Your relationships look like they are thought out. to me, it looks like your on the right track.
Just use the belongsToMany relationship instead of HasMany
Laravel has great documentation on this: laravel many to many
I am building an App with OctoberCMS and wanted to list related blog posts at the bottom of each post page basing on categories. I figured out rainlab_blog_posts table does not have a foreign key pointing to blog categories table. To achieve what wanted I thought of extending the blog_posts table and added category_id as foreign key using a plugin. I defined foreign key constraints in table migration. Everthing seem to be fine. My challenge is, How can I insert category id in posts table each time a new post is created. OctoberCMS create post backend has an option where the author assigns a category to a new blog post by selecting from list. Just don't understand how to pass this category id and insert into rainlab_blog_posts table in category_id field.
This is what I want to achieve in my API:
routes.php
use Rainlab\Blog\Models\Post;
Route::get('apiv1/related-posts/{postid}',function($id)
{
$post = Post::where('id',$id)->first();
$posts = Post::where('category_id',$post->category_id)
->where('id','!=',$id)
->orderBy('views','desc')
->get()->take(5);
return $posts;
});
Or if there is a better way of achieving this will appreciate. Cheers!
Hmm seems something wrong here,
As first I can see from Blog Plugin that Blog <=> Category it is MM relation so you will not find category_id in rainlab_blog_posts table as all relation is maintained by this mm-relation table rainlab_blog_posts_categories.
So I think you will select only one category for blog so you can get related blogs for only that category. [ assuming from your code and description ]
we can utilize relationships for that.
So your code can look like this
use Rainlab\Blog\Models\Post;
Route::get('apiv1/related-posts/{postid}', function($id)
{
$post = Post::where('id',$id)->first();
// we need this because there will be mm relation so
// we fetch first category and this will be based on
// [ name sorting - does not matter as there will be only one cat. ]
$firstCategory = $post->categories()->first();
// now we fetch only that post which are related to that category
// but we skip current post and sort them and pick 5 posts
$posts = $firstCategory->posts()
->where('id', '!=', $id)
->orderBy('views','desc')
->limit(5) // use limit instead of take()
->get();
// use limit instead of take() as we don't need extra data so
// just put limit in sql rather fetching it from db then
// putting limit by code - limit() is more optimised way
return $posts;
});
Good thing now you don't need to add that category_id field on rainlab_blog_posts. So, I guess now you Also don't need to worry about adding it during post insertion.
if any doubts please comment.
Incase you simply want to display related blog posts without having to customize the blog database, there is an OctoberCMS plugin called Related Articles
that requires rainlab blog plugin. It will display all the other articles belonging to the post's category.
On your Backend menu, go to settings,on System, go to Updates and Plugins.
Go to Install plugins. Search for plugin Related articles. The creator is Tallpro. Install it.
Go to CMS. On the page you want related articles, go to components and drag Related articles. Save and preview it. You can also fork the component to edit how you want your related articles to be displayed. You will find more information on the documentation.
If you encounter any problems feel free to inquire or you can check out the plugin reviews for problems faced and their solution.
I have a ProductCategory say "Dresses". Products are related to product category and images are related to Products. I want the list of records with both products, product images for a particular ProductCategory id using Laravel 5.2.
I tried:
$productCategories = ProductCategory::find(1)->products;
This give me related products, but now I want all the related images to the products in the result.
look for eloquent nested relationships eager loading. need to know what kind of relationships and relationship names you are using.
haven't tested but it should work something like this. if many to many relationship come by you may need to use a foreach to loop through finding related model of each.
$productCategories = ProductCategory::with('products', 'products.images')->findOrFail(1);