Laravel Check If Related Model Exists | Laravel Point

 

Laravel Check If Related Model Exists

You can check if a related model exists in Laravel by using the exists method on the relationship. Here is an example:

       $user = User::find(1);

if ($user->posts()->exists()) {
    // The user has at least one post
} else {
    // The user has no posts
}

In this example, the posts method returns the HasMany relationship, and the exists method checks if the relationship has any records in the database.

Previous Post Next Post

Contact Form