Updated April 10, 2023
Definition of Laravel Pivot Table
A pivot table is defined as the set of values arranged in a table form where every distinct value of the concerned table can be accessed in the form of a spreadsheet, database, and so on. It is available in one or multiple discrete functionalities. The function includes averages, sum, and another statistical process where the group of pivot tables works together in the chosen aggregate operation implied at the combined values. The blank field is created and it has appeared on the right end of the worksheet. The design and layout of the pivot table are appeared in the list by default. In this article, the pivot table function in Laravel and its implementation, creation, accessibility are explained.
What is Laravel Pivot Table?
The pivot table in laravel is a structured value that is grouped and aggregated in the individual items where the extensive table is obtained or accessed in the form of a spreadsheet, database, or other discrete functions. The summary and other functions in the pivot table include other statistical functions, sum, and average where the groups of a pivot table are chosen and the aggregate function is implied to grouped values. The field is created and it is displayed on the right end of the worksheet and the design and layout of the pivot table are appeared in the list by default. These fields in the pivot table are the building blocks of the pivot tables where all the fields in the list can be put into the layout which has four unique options rows, values, filters, and columns.
How to Create a Laravel pivot table?
In laravel, When working in a many-many relationship, the user has to choose the intermediate table and it is called a pivot table in terms of Laravel. The pivot attribute communicates with this intermediate table in controllers or models in Laravel. The admin in laravel has to define the role of every other user. So the user can have multiple roles and other inverse roles can be implied to another user which involves many-many relationships.
The user has to create around three tables roles, users, and intermediate table roles. The user role has the unique role id and user id which links both the role table and user with an intermediate table known as the pivot table in laravel.
The user function is defined in the following table,
1 | id | name | |
2 | 1 | Educba 1 | [email protected] |
3 | 2 | Educba 2 | [email protected] |
4 | 3 | Educba 3 | [email protected] |
5 | 4 | Educba 4 | [email protected] |
The role function is defined in the following table,
1 |
id | name | Created_at | Updated_at |
2 | 1 | Admin | 2021-04-27 23:00:32 | 2021-04-27 23:00:32 |
3 | 2 | Editor | 2021-04-27 23:00:32 | 2021-04-27 23:00:32 |
4 | 3 | Viewer | 2021-04-27 23:00:32 | 2021-04-27 23:00:32 |
5 | 4 | Subscriber | 2021-04-27 23:00:32 | 2021-04-27 23:00:32 |
When the user role function is combined, and the resultant pivot table is defined below,
1 |
id | User id | Role id | Created by | active | Created_at | Updated_at |
2 | 1 | Admin | 1 | 1 | 1 | 2021-04-27 23:00:32 | 2021-04-27 23:00:32 |
3 | 2 | Editor | 2 | 1 | 1 | 2021-04-27 23:00:32 | 2021-04-27 23:00:32 |
4 | 3 | Viewer | 2 | 1 | 0 | 2021-04-27 23:00:32 | 2021-04-27 23:00:32 |
5 | 4 | Subscriber | 3 | 1 | 1 | 2021-04-27 23:00:32 | 2021-04-27 23:00:32 |
Laravel Pivot Table Example
The user has two constant ids and they are in form of string and integer. The roles have two properties where ids are integer and name is in form string. The pivot table is retrieved when there is any active column that keeps on updated. The created_by column can be updated in the intermediate table. To define this function, the user has to create the model to fetch the column when defining the relationship.
Return
$this -> belongs ToMany (Role::class) -> with Pivot (‘active’, ‘created_by’);
The user should choose and access the attributes of pivot table in the controller and is stored in the below location. Application use \models \user
$user = user :: find (1);
Foreach ($user -> role as $ role)
{
echo $role -> pivot -> created_by;
Echo $role -> pivot -> active;
}
If the user has to retrieve the standard timestamps in the fields updated_at, created_at in the attribute of the pivot table which is used along with timestamps.
Return
$this -> belongs ToMany (Role::class)
->with Timestamps ()
->with Pivot( ‘updated_by’, ‘created_by’);
In Laravel, To rename the attribute options in a pivot table, it is better to fetch the relative name of the pivot functions like comments, order-items, subscriptions instead of the actual pivot name. The user has to rename the word pivot to other terms and use it as the identical character and the pivot attribute in the relationship.
public function subscription ()
{
return $this -> belongs ToMany (Podcast :: class)
->as ('value of subscription')
-> with Timestamps ();
}
The user can also imply the controllers like,
$podcast = $user -> podcast ();
foreach ( $podcasts as $podcast )
{
// instead of $podcast-> pivot -> created_at;
echo $podcast -> subscription -> created_at;
}
public function post()
{
return $this->belongsTo(Post::class, 'foreign_key', 'owner_key');
}
Few default models in Laravel, execute the following functions like has one, belongs to, through, morph one and another one to one relationship, one to many relationships, and other multiple relationships enable the user to choose the default model which is returned and the given relationship can be turned into null. The pattern followed in the pivot table can be defined as the null pattern in the particular objects and helps to eliminate the conditional checks in the code described. The user used to return an empty value if there are no strings attached to the after-executed model. The changes should be made to the below file,
Null file/ application/ model/ users.
public function user ()
{
return $this -> belongs To (User::class) -> with Default();
}
Conclusion
Hence these are a few examples and implementations of pivot tables in Laravel, and multiple relationships can be defined and referred to within the same database with simple commands.
Recommended Articles
We hope that this EDUCBA information on “Laravel Pivot Table” was beneficial to you. You can view EDUCBA’s recommended articles for more information.