Laravel: Hide attributes from model just in some routes
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Laravel: Conditionally Hiding Model Attributes Based on Routes
As a senior developer working with Laravel, we often encounter scenarios where the visibility of model attributes needs to be dynamic—meaning some data should be hidden for certain endpoints (like index) but fully exposed for others (like show). The default approach using the $hidden property in an Eloquent model applies universally, which creates inflexibility when dealing with route-specific requirements.
This post will explore how you can achieve conditional attribute hiding based on the current route or controller method, providing a robust and clean solution within the Laravel ecosystem.
The Limitation of Global $hidden Attributes
When you define attributes using the $hidden property in your Eloquent model, this setting is applied whenever that model is retrieved, regardless of which controller method or route is requesting the data.
For instance, if you hide thumbnail and status globally:
// In your Model
protected $hidden = [
'thumbnail',
'status',
];
This means that whether a user hits /posts (index) or /posts/{id} (show), the attributes will be filtered based on this model definition. This global approach lacks the necessary context to implement fine-grained security or display logic specific to the request type.
The Solution: Implementing Contextual Visibility in Controllers
Since Eloquent models manage the data structure, the most practical and secure way to control what is displayed is to handle the filtering logic within your controller methods, using route information as the context. This keeps the model clean while allowing for dynamic presentation layers.
We can check the current method being executed (e.g., index vs. show) and apply manual filtering or modification before returning the data.
Example Implementation
Let's assume you have a Post model with attributes that should be hidden from the index view but visible on the detail view:
// app/Models/Post.php
class Post extends Model
{
protected $hidden = [
'thumbnail', // Hide this by default if not needed for listing
'status', // Hide status in list view
];
// ... other model code
}
Now, let's implement the logic in the controller:
// app/Http/Controllers/PostController.php
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
// For the index route, we want to hide specific fields,
// even if they weren't in $hidden initially (demonstrating control).
$posts = Post::where('is_published', true)
->select([
'id', 'title', 'content', 'author_id' // Only select visible fields for index
])
->get();
return view('posts.index', compact('posts'));
}
public function show(Post $post)
{
// For the show route, we want all details, including those that might be hidden elsewhere.
$post->loadMissing(['thumbnail', 'status']);
return view('posts.show', compact('post'));
}
}
Best Practice: Using Accessors for Complex Logic
For more complex conditional hiding—especially when dealing with relationships or derived data—a highly recommended Laravel pattern is to use Accessors or Mutators. Instead of hiding the raw attribute via $hidden, you can create a method that controls the output based on the context.
If you must hide data from the model layer entirely, ensuring your Eloquent setup adheres to principles discussed by the Laravel team regarding data encapsulation will help maintain consistency across your application. Understanding how Eloquent manages relationships and data retrieval is crucial for building scalable applications; for more deep dives into these concepts, check out the official documentation at https://laravelcompany.com.
Conclusion
Conditionally hiding model attributes based on routes cannot be solved purely by static model properties like $hidden. The solution lies in leveraging the controller layer as the context manager. By performing explicit select() queries or loading specific relations within your route-specific methods (index, show), you gain precise control over the data exposed to the view, ensuring that your application remains secure and efficient while adhering to Laravel's principles of clean separation of concerns.