Laravel eloquent where relationship

php - Laravel where on relationship object - Stack Overflow

laravel eloquent where relationship

Laravel One To One Eloquent Relationships with Example From Scratch is today's leading topic. Laravel is famously known as an Eloquent. Eloquent ORM seems like a simple mechanism, but under the hood, there's a lot of But you can do it on Eloquent relationship level. Event::whereHas('partecipants', function ($query) { $query->where('IDUser', '=', 1 ); })->get();. Read more at immobilier-haute-garonne.info#eager-loading.

So it will return all the deliveries with the products. Also the deliveries that has no products. Maybe you want only deliveries that has at least one product, the whereHas or another methods are available I think, usually, there aren't deliveries with product, but maybe you want deliveries with or without products, for example just a letter.

Few days ago someone ask for the same I that case maybe is better to use join, leftjoin, rightjoin Or if you you use them, use them with paginate Xthat way the whereIn array wil have X values.

laravel eloquent where relationship

Cancel kokokurak replied 4 years ago I tried this: Cancel iClosedz replied 4 years ago So I think you need to use raw sql. Cancel ekaitzastiz replied 4 years ago just do it with join.

Cancel IanSirkit replied 4 years ago Maybe I am missing what you are trying to do but did you set up the relationships in the model using hasOne, hasMany, belongsTo or any of those?

laravel eloquent where relationship

If not that is the way to do it. Cancel kokokurak replied 4 years ago IanSirkit said: Maybe I am missing what you are trying to do but did you set up the relationships in the model using hasOne, hasMany, belongsTo or any of those? To define the inverse of a hasMany relationship, define a relationship function on the child model which calls the belongsTo method: An example of such a relationship is a user with many roles, where the roles are also shared by other users. For example, many users may have the role of "Admin".

laravel eloquent where relationship

To define this relationship, three database tables are needed: Many-to-many relationships are defined by writing a method that returns the result of the belongsToMany method. For example, let's define the roles method on our User model: However, you are free to override this convention. You may do so by passing a second argument 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: To continue our user roles example, let's define the users method on the Role model: Since we're reusing the belongsToMany method, all of the usual table and key customization options are available when defining the inverse of many-to-many relationships.

Retrieving Intermediate Table Columns As you have already learned, working with many-to-many relations requires the presence of an intermediate table. Eloquent provides some very helpful ways of interacting with this table. For example, let's assume our User object has many Role objects that it is related to. After accessing this relationship, we may access the intermediate table using the pivot attribute on the models: This attribute contains a model representing the intermediate table, and may be used like any other Eloquent model.

By default, only the model keys will be present on the pivot object. If your pivot table contains extra attributes, you must specify them when defining the relationship: However, you are free to customize the name of this attribute to better reflect its purpose within your application.

laravel eloquent where relationship

For example, if your application contains users that may subscribe to podcasts, you probably have a many-to-many relationship between users and podcasts. If this is the case, you may wish to rename your intermediate table accessor to subscription instead of pivot. This can be done using the as method when defining the relationship: For example, we may define a Role which uses a custom UserRole pivot model: For example, a Country model might have many Post models through an intermediate User model.

laravel eloquent where relationship

In this example, you could easily gather all blog posts for a given country. Let's look at the tables required to define this relationship: After finding the matching user IDs, they are used to query the posts table. Now that we have examined the table structure for the relationship, let's define it on the Country model: Typical Eloquent foreign key conventions will be used when performing the relationship's queries.

If you would like to customize the keys of the relationship, you may pass them as the third and fourth arguments to the hasManyThrough method. The third argument is the name of the foreign key on the intermediate model. The fourth argument is the name of the foreign key on the final model. The fifth argument is the local key, while the sixth argument is the local key of the intermediate model: One To One Polymorphic Table Structure A one-to-one polymorphic relation is similar to a simple one-to-one relation; however, the target model can belong to more than one type of model on a single association.

For example, a blog Post and a User may share a polymorphic relation to an Image model. Using a one-to-one polymorphic relation allows you to have a single list of unique images that are used for both blog posts and user accounts. First, let's examine the table structure: Model Structure Next, let's examine the model definitions needed to build this relationship: For example, to retrieve the image for a post, we can use the image dynamic property: In our case, that is the imageable method on the Image model.

So, we will access that method as a dynamic property: One To Many Polymorphic Table Structure A one-to-many polymorphic relation is similar to a simple one-to-many relation; however, the target model can belong to more than one type of model on a single association.

How to make relationships in models - Laravel

For example, imagine users of your application can "comment" on both posts and videos. Using polymorphic relationships, you may use a single comments table for both of these scenarios.

Search on Eloquent Relationships

First, let's examine the table structure required to build this relationship: For example, to access all of the comments for a post, we can use the comments dynamic property: In our case, that is the commentable method on the Comment model. For example, a blog Post and Video model could share a polymorphic relation to a Tag model.

Using a many-to-many polymorphic relation allows you to have a single list of unique tags that are shared across blog posts and videos.

The Post and Video models will both have a tags method that calls the morphToMany method on the base Eloquent class: So, for this example, we will define a posts method and a videos method: For example, to access all of the tags for a post, you can use the tags dynamic property: In our case, that is the posts or videos methods on the Tag model.

Eloquent: Relationships - Laravel - The PHP Framework For Web Artisans

So, you will access those methods as dynamic properties: However, you may wish to decouple your database from your application's internal structure. In that case, you may define a "morph map" to instruct Eloquent to use a custom name for each model instead of the class name: Querying Relations Since all types of Eloquent relationships are defined via methods, you may call those methods to obtain an instance of the relationship without actually executing the relationship queries.

In addition, all types of Eloquent relationships also serve as query buildersallowing you to continue to chain constraints onto the relationship query before finally executing the SQL against your database.

For example, imagine a blog system in which a User model has many associated Post models: Dynamic Properties If you do not need to add additional constraints to an Eloquent relationship query, you may access the relationship as if it were a property.

For example, continuing to use our User and Post example models, we may access all of a user's posts like so:

  • how to add a where clause to a relationship
  • Eloquent Relationships Cheat Sheet
  • where() on with() relation