Laravel many to many 2 level down pivot table - laravel

I am trying to build a Survey Module in Laravel where user can create survey, assign questions and options to the questions in survey.
survey can have multiple questions
survey question can have multiple options
To meet the above requirement I created the following models & tables
Model: Question
Table: questions
| id | question |
| -------- | -------------------------|
| 1 | How is the performance? |
| 2 | How did you know about us|
Model: Option
Table: options
| id | option |
| --- | --------- |
| 1 | Good |
| 2 | Bad |
| 3 | Google |
| 2 | SMS |
Now the relationship between questions and survey will be stored in pivot table
Model: SurveyQuestion
Table: survey_questions
| id | survey_id| question_id |
| ---| -------- |-------------|
| 1 | 1 |1 |
| 1 | 1 |2 |
Upto this point I know how to store data into pivot table using attach/sync.
Now the problem is As per requirement each survey question might have multiple options so I created another pivot table survey_question_option in which I am using survey_questions primary key as foreign key.
Model: SurveyQuestionOption
Table: survey_question_options
| id | survey_question_id| option_id |
| -- | ----------------- |-----------|
| 1 | 1 |1 |
| 1 | 1 |2 |
Now my question is that is it correct to use pivot table primary key as foreign key into another table?
If yes then how can I store data into survey_question_options table using laravel relationships?
If no then what is the better solution?

In this case you can create a Custom Intermediate Table Model (Custom Pivot)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class SurveyQuestion extends Pivot
{
public function options()
{
//You need to create a pivot pivot table survey_question_option
//with (survey_question_id, option_id)
return $this->belongsToMany(Option::class, 'survey_question_option');
}
}
Your models will need to recognize this new Pivot Model with the method using(). Here is an example with Survey Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Survey extends Model
{
/**
* The users that belong to the role.
*/
public function questions()
{
return $this
->belongsToMany(Question::class)
->using(SurveyQuestion::class);
}
}
And them you will be able to access via eloquent like this
$survey->questions->pivot->options
For more information you can check the documentation here:
https://laravel.com/docs/8.x/eloquent-relationships#defining-custom-intermediate-table-models

Related

how to make the relation between many to many and one to many?

Suppose we have 3 entities named A, B, and C. And Relations between these Entities are something like this:
A -- one to many --> B
B -- many to many --> C
Example tables:
A
| id |
| -- |
| 1 |
| 2 |
B
| id | a_id |
| -- | -- |
| 1 | 1 |
| 2 | 2 |
B_C
| b_id | c_id |
| -- | -- |
| 1 | 1 |
| 2 | 2 |
C
| id |
| -- |
| 1 |
| 2 |
If we wanna make the relationship between A and C in laravel, what should we do? is it possible at all?
For these situations, you can use Has Many Through relationships and for more information, you can follow the laravel documentation.
to get the relation between your A and C model you can use has many though like this:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class A extends Model
{
public function c()
{
return $this->hasManyThrough(
C::class,
B::class,
'a_id', // Foreign key on the a table...
'b_id', // Foreign key on the b table...
'id', // Local key on the a table...
'id' // Local key on the b table...);
}
}
The first argument passed to the hasManyThrough method is the name of the final model we wish to access, while the second argument is the name of the intermediate model.

How to get data from nested relationship in Laravel 8 Eloquent?

How to make a deep relationship with eloquent? I'm trying to display the data like this :
+------------------------------------+
| Main ID | Name | SN | Last Event |
|------------------------------------|
| 12 | James | j89 | RIGHT |
+------------------------------------+
The "Last Event" column is based on the latest data on "Tracks" table but the value "RIGHT" is relation between the "Tracks" table with "Events" table. So, in this table, the latest record in the tracks table is id of 9 with event_id of 12, the event_id of 12 in the Events table has name column that contained value RIGHT. That value that I want to grab it to display in front end. this is my table in database.
Main table
+---------------+
|id | name | sn |
|---------------|
|12 | James| j89|
+---------------+
Tracks table
+-------------------------------------+
|id | main_id | event_id | created_at |
|-------------------------------------|
| 5 | 10 | 10 | 2021-10-12 |
| 9 | 10 | 12 | 2021-11-20 |
+-------------------------------------+
Events
+----------+
|id | name |
|----------|
|10 | LEFT |
|12 | RIGHT|
+----------+
If I use has one relationship with latestOfMany() method, it didn't reach to the events table. How do I reach it to the Events table to grab the value in Events table through the latest data of Tracks table based on main_id? thanks!
you can use Eager Loading to load deep in your relation, if you setup your relation correctly:
class Main extends Model
{
public function latestTrack()
{
return $this->hasOne(Track::class,'main_id')->latestOfMany();
}
}
class Track extends Model
{
public function event()
{
return $this->belongsTo(Event::class,'event_id');
}
}
now you can get the structre you need:
$main=Main::with('latestTrack.event:id,name')->find(12);

How to write Eloquent relation for one table to another table two columns?

I am trying to join two columns with a table.
Here is my table structure for tasks table
| id | assigned_by | assigned_to |
| :--- | :---: | ---: |
| 1 | 1 | 2 |
| 2 | 1 | 3 |
Here is the table structure of users table.
| id | name | email |
| :--- | :---: | ---: |
| 1 |varun | me#gmail.com |
| 2 |mark | mark#gmail.com|
I tried below code in Task Class it didn't work
public function user()
{
return $this->belongsTo(User::class);
}
Eloquent will automatically determine the proper foreign key column on the User model. By convention, Eloquent will take the "snake case" name of the owning model and suffix it with _id. So, for your example, Eloquent will assume the foreign key on the User model is user_id.
However, your foreign_key is assigned_by and assigned_to, so you need to specify the foreign_key:
public function assignedUser()
{
return $this->belongsTo(User::class, 'assigned_to');
}
public function assignedByUser()
{
return $this->belongsTo(User::class, 'assigned_by');
}

Connect a table to many to many table

I have a many to many table from goods and companies and i want to connect it with a one to many table
company_good
+----------------+------------+
| company_ID | good_ID |
+----------------+------------+
| 1 | 1 |
| 2 | 1 |
| 2 | 2 |
+----------------+------------+
sales
+----------+----------------+--------------+----------+
| id | date | company_id | good_id |
+----------+----------------+--------------+----------+
| 1 | 2019-02-01 | 1 | 1 |
| 2 | 2019-02-01 | 2 | 1 |
| 2 | 2019-02-01 | 1 | 1 |
+----------+----------------+--------------+----------+
what i want to ask is are there any naming column on sales based on laravel rule? what i know is when a table posts want to connect it to table users the naming on table post should be user_id. but in this case how do i name it?
First off, you'll definitely want to use Eloquent for this.
The column names can be whatever you want them to be. When you set up a many to many model method you can define the related column names in there
Here's an extract from the Laravel documentation:
In addition to customizing the name of the joining table, you may also
customize the column names of the keys on the table by passing
additional arguments to the belongsToMany method. The third argument
is the foreign key name of the model on which you are defining the
relationship, while the fourth argument is the foreign key name of the
model that you are joining to:
return $this->belongsToMany('App\Role', 'role_user', 'user_id',
'role_id');
Read more at https://laravel.com/docs/5.8/eloquent-relationships#many-to-many
I suggest to add an id column to company_good table and name corresponding model as CompanyGood and instead of company_id and good_id in sales table use company_good_id column

Query data based on pivot table

I have a database design which essentially takes the following form (I have removed a lot of fields as they are not really needed for demonstration purposes). I have a users table
users
+----+---------------+-----------------+
| id | name | email |
+----+---------------+-----------------+
| 1 | ProjectA | Something |
+----+---------------+-----------------+
A user can have many projects
projects
+----+---------------+-----------------+-----------------+
| id | name | description | user_id |
+----+---------------+-----------------+-----------------+
| 1 | ProjectA | Something | 1 |
+----+---------------+-----------------+-----------------+
So that is straight forward enough and very easy to query. If I want all projects for the logged in user I can do
$loggedInUser = Auth::user()->getFirstName() . ' ' . Auth::user()->getLastName();
$loggedInUserId = User::where('userName', '=', $loggedInUser)->first();
$projectss = Project::all()->where('user_id', $loggedInUserId);
This is where things get a little more tricky. I have a pivot table for a users groups. It is essentially this
users_user_groups
+----+---------------+-----------------+
| id | user_id | group_id |
+----+---------------+-----------------+
| 1 | 1 | 2 |
+----+---------------+-----------------+
I then have a user_groups table
user_groups
+----+---------------+
| id | group_name |
+----+---------------+
| 1 | Group A |
+----+---------------+
If I want to find what groups a user is a part of I can do
$userGroups = Auth::user()->getGroups();
My question is this. Above, I demonstrate how I can get all projects for a user. Now I know there is a user_group called Group A. What I essentially want to do is get all projects where the user is apart of Group A. So if 5 users create a project and they are all in Group A, then I should be returned 5 projects.
How would I go about doing something like this?
Thanks
Firstly you can get user id using simply Auth::user()->id
Assuming you are trying to get all the projects that belongs to users of a group You can do this.
$group = Group::with('users.projects')->find(1);
Group model
public function users()
{
return $this->belongsToMany('App\User');
}
User Model
public function projects()
{
return $this->hasMany('App\Project');
}
Of course you would have to make the appropriate relation methods depending on the forign/primary keys you used.

Resources