Laravel RelationshipAn Eloquent relationship is a very important feature in Laravel that allows you to relate the tables in a very easy format. One to one relationshipOne to one relationship provides the one-to-one relationship between the columns of different tables. For example, every user is associated with a single post or maybe multiple posts, but in this relationship, we will retrieve the single post of a user. To define a relationship, we need first to define the post() method in User model. In the post() method, we need to implement the hasOne() method that returns the result. Let's understand the one to one relationship through an example.
The above screenshot shows that the user_id column is added successfully.
In the above code, we have implemented the hasOne() method that contains the single argument, i.e., name of the related model. By default, the Post considers the user_id as a foreign key. The post() method searches the posts table as we have mentioned the namespace 'App/Post' and look for the column user_id. We can override this convention by providing a foreign key as a second argument. It can be re-written as: return $this->hasOne('App\Post',foreign_key)
The above code is finding the user with an id number 1 and then implementing the post to find the post of the user having user_id equal to 1. Output Inverse RelationInverse relation means the inverse of the one-to-one relationship. In the above, we have retrieved the post belonging to a particular user. Now, we retrieve the user information based on the post. Let's understand this through an example.
Output One-to-many relationshipLaravel also provides a one-to-many relationship.
Output Many-to-Many relationshipA many-to-many relationship is more complicated than a one-to-one relationship and a one-to-many relationship. To define the many-to-many relationship, we need to create a pivot table. The pivot table is basically a look up table that relates the two tables. For example, a user may have different roles where the roles can be shared by other users, like many users can have the role of 'Admin'. To define the relationship between the users and roles, we need to create the three tables, user, roles, and role_user. In our database, the user table is already created; we need to create two tables, i.e., roles table and pivot table (roles_user).
The above screen shows that the roles table has been created. The create_roles_table.php has been created in the database/migration directory. The structure of this file is given below: In the above code, we have added the new column named as 'name'. The 'name' column defines the name of the role of a user.
The above screen shows that the roles_user table has been created. The structure of the create_roles_user_table is given below: In the above code, we have added two new columns, user_id, and role_id.
Data available in the roles table: Data available in the users table: Data available in the roles_user table:
web.php
In the above code, belongsToMany() method contains two parameters, 'App\Role', which is the namespace to use the Role model, and 'roles_user' is the name of the pivot table that relates two tables. The belongsToMany() method can also be written as: belongsToMany('App\Role','roles_user','user_id','role_id'); The above line contains two more parameters, user_id, and role_id. The user_id is the foreign key for the users table, and role_id is the foreign key for the roles table. Output Accessing the intermediate/pivot tableIn many-to-many relationship, we create the pivot or intermediate table. Now, we will see how to retrieve this pivot table. In the above model, we are retrieving the user having an id equal to 1. Then, using the foreach loop, we are retrieving the role model and assigned in the pivot attribute. If we want to retrieve the specific column from the pivot table, web.php Output Has Many ThroughThe 'has many through' relationship provides a convenient way of accessing the distant or intermediate relations. For example, we have three tables, users, posts, and country table. Now, we want to find the posts belonging to that country through the User model. Let's understand through an example.
In the above code, we have added a new column in users table. To migrate the above changes in the database, php artisan migrate
Output Polymorphic relationshipOne-to-many (Polymorphic) The Polymorphic relationship is similar to the one-to-many relationship. When a single model belongs to more than one type of model on a single association is known as one-to-one polymorphic relationship. For example, if we have three tables, posts, users, and photo table, where photo table represents the polymorphic relation with the users and posts table. Let's understand this relationship through an example.
Open the create_photos_table.php file created in the migrations folder. In the above code, we have added three columns, path, imageable_id, and imageable_type. The path determines the path of the image, imageable_id is the id value of the user or post, and imageable_type is the class name of the model.
Data available in a posts table Data available in users table: Data available in photo table:
Output Inverse of one-to-many (polymorphic) relationship In this topic, we will perform the inverse operation of the one-to-many polymorphic relationship. Till now, we found the image of the users and posts, now we find out the owner of the image. Let's understand through an example. We need to create the route in web.php file. In the above code, Photo::findOrFail($id) method determines whether the photo of a given id exists or not. If exists, then it returns the details of the image through the statement, '$photo->imageable'. Output The above output shows the details of the image. Many-to-many polymorphic relationship In a many-to-many polymorphic relationship, a target model consists of unique records that are shared among the various models. For example, a tag table shares the polymorphic relation between the videos and the posts table. A tag table consists of the unique list of tags that are shared by both the tables, videos, and the posts table. Let's understand through an example.
Open the migration file of the Audio table named as 'create_audio_table'. In the above code, we have created the name column in the audio table by using the command $table->string('name');. Open the migration file of the Tag model named as 'create_tag_table'. In the above code, we have created the name column in the tags table by using the command $table->string('name');. Open the migration file of the Taggable model named as 'create_taggables_table'. In the above code, we have added three new columns in the taggables table, i.e., tag_id, taggable_id, and taggable_type. The tag_id represents the id of the tag table, taggable id represents the id of the model table, and taggable type represents the name of the class.
Data available in an audio table: Data available in post table: Data available in tags table: Data available in taggables table:
Audio.php Post.php
Output When accessing the route of the post, the output would be: When accessing the route of the audio, the output would be: Inverse of many-to-many (polymorphic) relationship In a many-to-many polymorphic relationship, we found the tags belonging to the post and audio model. But, in an inverse relationship of many-to-many (polymorphic), we will find out all the posts and audios that belong to a particular tag. Let's understand through an example.
Tag.php In the above code, we define two methods, posts() and audios(). In the posts() method, we are retrieving all the posts that belong to a specified tag. In the audios() method, we are retrieving all the audios that belong to a specified tag.
Output Next TopicLaravel Tinker |