Multi categories posts - laravel-5

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

Related

Database and code design for category and sub category in laravel

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.

Laravel 3rd level relationship

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.

laravel define relation over multiple tables

I have a table customers with the fields id, name and so on.
One table doctors with the fields id, name.
Then there is one table subject_areas which has all subject areas which a doctor can have. The fields are id, text.
So, each doctor can have multiple subject areas. There is one pivot table doctor_subject which is a belongsToMany relation.
Here is my problem: A customer can have multiple doctors, but only for a specific subject area. I tried it with a new table customer_doctor with the fields id, customer_id and doctor_subject_id. But how do i map this in Eloquent?
Issue was in relation between tables. After chat clarification this came out as solution:
Html form is written in a way that customer first choose doctor, then depending on selection choose several of his available areas.
In that scenario customer needn't to be related to areas directly and should be related to areas only over relation with doctor.
Also as side note, if needed deeper relations, models on pivot tables could be created and used as well.

Eloquont related data

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);

Help with Codeigniter and MVC model pattern

I am creating a site much like a wordpress blog whereby the front page will display a post loop: with the post_summary, author info, and tags.
I have four tables:
posts | users | tags | tag relationships
to display all the results i would need to do multiple JOINs for in the SELECT statement
However, to stay with the MVC pattern, there should be a model for each table ( or object ?). So my question is: If I were doing a SELECT all, how would I do this and still keep with the MVC pattern?
To get all the required info for the post, I need the author_id to get my info from the users table AND I need the post_id to get the tags (and so on). If all of my queries are in different Models, what is the best way to perform the query?
Do I make one Model that does all of the JOINS and just use it? Should I load Models from the view? Or should I do additional query work in the Controller?
I think you have a misunderstanding of the purpose of Models. Models are to deal with data in your database and are not limited to 1 table per model. If you are creating a blog, you will really just need one model. Take a look at the tutorial on the codeigniter website, http://codeigniter.com/tutorials/watch/blog/, and reread the user guide for models, http://codeigniter.com/user_guide/general/models.html .
You may be getting MVC confused with an ORM
Do not make a model for the joins. As answered by #Johnny already, a Model and a table do not need to have a one-to-one relationship. In this case you are displaying blog entries, so you could have a Model named "Blog", with a method "GetList()". It is not relevant whether that query reaches out to multiple tables.
Think about it conceptually. Your are displaying blog entries, and each blog entry has other objects associated to it (such as a user id). Try to think domain-driven, not table-driven.
Make a model for the JOINS. It can include post_summary, recent_comments, etc.
Just use it in the front_page controller, the side_bar controller (for recent_comments, etc).
It would be better not to put query work directly in views or controller and views should not need to access the models IMO.

Resources