laravel parent child relationship with three tables - laravel

i am new to Laravel i need help in building a relation with models, i have coach model which has many clients and client belong to Coach and i can perform operation on clients like this Auth()->user()->clients()->create($request->all()); now i want to add courses in way that i could use something like Auth()->user()->clients()->courses()->create($request->all()); how should i make a relation for this. what will be the best approach.

Actually you can't do ->clients()->courses() because the ->clients() will returns you a lot clients of user so ->courses() doesnt know the course of which client do you want to get.
You can create a relationship between the Couch and Course(add couche_id to courses table) model so then you can do
Auth()->user()->courses()->create($request->all());

Related

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

Laravel: Table structure for multiple users types, polymorphic relationships

In my site (api using laravel 5.6 and laravel passport) I have two types of users (Teachers and Students), in the future there will be more. The teacher and student entities are very different, meaning that if I keep them all in one table, the table will be long and many fields will have a null value. Right now I have one Users table with common fields and two other tables (Teachers and Students) to which I have setup a polymorphic relationship from user. My question is if this is a good approach, or if there are other ways to handle this more elegantly?
I would create 1 table for Teachers and 1 table for Students and not use the Users table/model. This way you can keep them completely separate and not worry about adding more types of users in the future. Continually trying to fit new users into your existing Users model, which would be shared, is a headache. I made this same mistake when I started and eventually had to rework the project.
There are plenty of guides for Laravel multi-auth / multi-user online.
Here are a couple to help you get started:
https://medium.com/hello-laravel/multiple-authentication-system-laravel-5-4-ac94c759638a
https://www.codementor.io/okoroaforchukwuemeka/9-tips-to-set-up-multiple-authentication-in-laravel-ak3gtwjvt
Also, there are cases where it makes sense to use the User model for multiple types of users. For example, you may have multiple roles for a user where most/all of the fields are the same (not your scenario). In this case, you can assign a 'role' to each User and the check the roles for actions (e.g. add middleware to prevent roles from accessing various routes). Here is an example:
https://medium.com/#ezp127/laravel-5-4-native-user-authentication-role-authorization-3dbae4049c8a
Since you said the teacher and student entities are very different, you should keep them separate.

Parse Relations: which Class should own it?

Using Parse.com "Relations", how do you determine which of the 2 classes should own the Relation?
For example think of WhatsApp groups.
Should User have a relation listing all the groups it subscribes to?
Or should Group have a relation listing all the users in the group?
And, does it make sense to have a relation in each? Duplicating the data?
Depends on if you want to store some metadata in one of the Classes. It is explained quite nicely in this part of the document:
https://parse.com/docs/relations_guide#manytomany-relations
The decision point here is whether you want to attach any metadata to
the relationship between two entities. If you don’t, Parse Relation or
using Arrays are going to be the easiest alternatives. In general,
using arrays will lead to higher performance and require fewer
queries. If either side of the many-to-many relationship could lead to
an array with more than 100 or so objects, then, for the same reason
Pointers were better for one-to-many relationships, Parse Relation or
Join Tables will be better alternatives.
In the whatsapp you have given, Since you have access to user, i think it should be more like what are the groups that user belongs to. Read the many to many relations assuming group as book and users as author. It will make sense

Magento save many to many models

I have created a custom module with 3 models (student, room, studentroom).
The studentroom is used to store the relation between student & room (manytomany).
I have maked the form + grid to the students and rooms and i can create both of them. i want now to add a multiselect in the student form to select the rooms (until now its ok). When i save or edit my student how can i save or display also the association in studentroom.
Anyone has an idea ? A module with the same functionalities ?
Thx for advance
Magento's ORM has no built-in methods for the sort of one to many and many to many relationships you're describing above. As such, it's up to each individual developer to implement their own save methods that (before or after calling parent::save()) handle any extra relationships an object might have.

Resources