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)
Related
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..
How to add multiple tables in one model without creating new model.
Is this standard practice to use same in laravel-lumen.
Or should I create 50 models to work on 50 tables.
You can't use multiple table in one model. Each model is written for a specific table.
https://laravel.com/docs/5.7/eloquent#introduction
Laravel uses Eloquent Object Relational Mapper. So each model is like a class representation having methods to do query underneath.
You have to create 50 models if you have 50 tables, or at-least for the tables you are using in your application. TO save time, go for plugins like this which would generate eloquent models based on your database structure. Also check another plugin if it helps.
It's not possible.
Each table should have their own model. But it isn't necessary all the times to make a model for each table. Investing some time on making model will help a lot in future work.
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
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?
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