---
title: 'Laravel Tip: Use withAttributes to Auto-Apply Conditions When Creating Related Models'
source: 'https://youtube.com/watch?v=F1t6AHEupM8'
video_id: 'F1t6AHEupM8'
date: 2026-08-14
---

# Laravel Tip: Use withAttributes to Auto-Apply Conditions When Creating Related Models

> Source: [Laravel Tip: Use withAttributes to Auto-Apply Conditions When Creating Related Models](https://youtube.com/watch?v=F1t6AHEupM8)

## Summary

This video demonstrates a Laravel Eloquent tip: using a separate method with a condition for relationships, and the withAttributes() function to automatically apply that condition when creating related models.

### Key Points

- **Conditional Relationship Method** [00:00] — You can define a relationship method with a where condition, and it will be automatically applied when accessing the relationship.
- **Manual Attribute Setting** [00:12] — When creating related models, the condition is not automatically applied, so you must manually set the attribute.
- **withAttributes() Function** [00:26] — The withAttributes() function applies the same condition automatically when creating related models, eliminating manual steps.

## Transcript

When defining eloquent relationships, you may provide a separate method with condition. And then if you do user with that method, that where condition would be automatically applied.
But also, there is a similar trick for creating related objects. The problem with this approach, if you do user feature post create, you can do that, but then you need to provide featured manually, because that condition wouldn't work.
So did you know there is a function called with attributes? that would do the same wear condition as I showed previously, but also would allow you to do something like this. User feature posts, you provide only the title, and then featured would
be automatically applied as well. For more Laravel tips and longer tutorials, subscribe to my channel Laravel Daily.
