Laravel 5: What's the best Eloquent Relationship for this relation? Tasks can have one Topic AND one Project AND one - laravel-5

I have three models:
Task
Project
Topic
A Task can be associated with a Project or a Topic or to both.
Is there any type of eloquent relationship that I can use to achieve this type of relation or do I have to use the Polymorphic Many to Many?
I don't want to use the standard belongsTo with foreign key because I might extend it to other models.
Thanks!

Eloquent Polymorphic relations docs defines a "type" column so your single Task could not belong to two models at once. That mean, Task can morph to Topic OR Project.
A way to solve problem you answered yourself - belongsTo, hasOne, hasMany and so on.

Related

When polymorphic relation shouldn't be used?

Is it okay to use a polymorphic relation when there are lets say 6 common columns and 2 columns with different names?
I need to track car maintenance and refueling.
maintenances - table
-date
-km_driven
-info (refers to maintenance info )
refuelings - table
-date
-km_driven
-amount (refers to amount in liters)
So, should i use polymorphic relationship or not? Is it ok if there are more different columns per model?
IMHO for your case I will go for single table inheritance (STI), but you need a library like this tightenco/parental or this one Nanigans/single-table-inheritance.
Laravel codebase has no support for STI without an external library. You can try to use polymorphic relation to solve your case but you will end up with 3 different tables. I think you want to use a single table with two or even three models so my advice is to try one of the STI library above.
STI is appropriate when your models have shared data/state. Shared behavior is optional because can be defined per Model. An example could be different type of vehicle Models: Car, Truck, Bike etc..
With Polymorphic Relations instead, a model can belong_to several models with a single association. This is useful when several models do not have a relationship or share data with each other, but have a relationship with the polymorphic class. An example could be the Comment Model that can belongs to other Models: User, Post, Image etc..

Using 3NF in Laravel's ORM

I am trying to build a simple Laravel application. My data model looks like the following:
ENTITIES:
Project, Requirements, ProjectRequirementStatus
RELATIONSHIPS:
A Project has many Requirements
A requirement belongs to many Projects as a "ProjectRequirement"
A "ProjectRequirement" has one ProjectRequirementStatus
A ProjectRequirementStatus belongs to many Projects
TABLES:
projects
requirements
project_requirements
project_requirement_statuses
MODELS:
Project, Requirements, ProjectRequirementStatus
My question IS:
Is it improper to create a model for a relationship class? In this case, I would need to create a ProjectRequirement model and define the relationship to the ProjectRequirementStatus class.
I'm confused because most of my pivot tables include IDs of the two tables they are joining in a Many to Many, and typically, no additional relationships.
Am I thinking about this the wrong way? Are there "best practices" in terms of when a Model is created versus when it's not needed?
Using the 3NF in Laravel, you do not have to make models for the relationships. Laravel provides the Eloquent ORM which will provide the relationships without having to make the pivot tables models.
The Eloquent ORM also provides you a way to access data on pivot tables. (Defining The Inverse Of The Relationship)

Eloquent laravel relationship

I have a project and I want to do it, but I got a problem in relationships.
Project Details:
The system consists of a set of courses, each course offered by one or more trainers, and each trainee can enroll in one or more courses.
My question :
How do I make relationships in Laravel? So that I can get all courses of the trainee with the course information and the trainer who provided the course.
My question : How do I make relationships in Laravel?
Laravel uses eloquent to define models and relationships.
In your case, i think you are looking for a One to Many and Many to Many relationship:
Course has many traniners
Trainee belongs to many courses
Course belongs to many trainees
TIP: In Many to Many relationship is needed a third table with both primary key.
Just look the documentation.
https://laravel.com/docs/master/eloquent-relationships

How to create two sided many to many polymorphic relationships in Laravel?

I have multiple models which can and can be followed by each other i.e. Business, Agency, Vendor, User.
The schema of the above scenario is as follows:
id follower_id follower_type followable_id followable_type
Is there any relationship for the above case in Laravel? How can I create relationship and use eloquent methods?

Laravel - Creating entity relationships when writing a migration

I am currently learning Laravel, and I have a query regarding the best practice for creating entity relationships whilst writing a migration as I am confused as to how best do this.
Lets say you are writing an entity schema that you know will have a Many to Many relationship or a One to Many relationship or even a foreign key, etc.
Are you meant to code the relationship into the migration itself, or after it?
All the tutorials I've seen up to now seem to gloss over how to make the migration know that Entity A will have relationship with Entity B and put the relationships in the models using hasMany(), etc
This confuses me, are you meant to put the relationship structure into the migration; or is it better to create your migration and then make your relationship in the model?
Thanks now

Resources