Merging Static Data with Database Queries in Laravel Dropdowns
When building a form with a <select> element, you often need to pull available options from a database while also providing a static choice that doesn't exist in your tablesâlike an "Others" option. This is a common pattern for capturing data that falls outside predefined categories without cluttering your schema.
However, mixing raw PHP array manipulation with Laravel's Eloquent collections can lead to type errors if not handled correctly. The error Trying to get property of non-object typically occurs when you attempt to access an object property on something that isn't an instance of your model. In the scenario described, this happens because $competition_all is a collection of models, but the logic attempts to treat it as a simple array or misaligns the iteration variables.
To add a static "Others" option without inserting a row into your competitions table, you don't need complex array pushing that breaks your model integrity. Instead, you should construct the dropdown data structure explicitly before passing it to the view.
Using collect() and merge()
The most robust approach is to start with the database results and then merge a static array into a new collection. This keeps your Eloquent models intact while allowing you to append arbitrary data.
// In your controller
$competition_all = Competition::all();
// Create a new collection from the query result
$dropdownOptions = collect($competition_all)->map(function ($competition) {
return [
'value' => $competition->id,
'label' => $competition->name,
];
});
// Append the static 'Others' option
$dropdownOptions->push([
'value' => 'others',
'label' => 'Others',
]);
// Pass to view
return view('register', ['competitions' => $dropdownOptions]);
In this implementation, we avoid using array_push directly on the collection returned by Competition::all(). While Eloquent collections extend arrays and technically support array methods, pushing a raw associative array like ['name' => 'Others'] onto a collection of models creates a heterogeneous list. If your view loop expects model properties (like $item->name), accessing those on the "Others" item will fail because it is now an array, not a model.
By mapping the query results first into a standardized structure (associative arrays with value and label keys) and then pushing the static option in the same format, you ensure uniformity. The view can iterate over $competitions using a simple foreach loop:
@foreach ($competitions as $option)
<option value="{{ $option['value'] }}">{{ $option['label'] }}</option>
@endforeach
Avoiding Type Confusion in Iteration
A frequent pitfall, evident in the original error report, arises from how the loop variable is assigned. If you iterate directly over a collection of models and then try to access properties, every item must be an object. When you mix a model with a plain array via array_push, the collection becomes mixed-type. Accessing $competition_games->name on the "Others" entry throws the exception because $competition_games is an array, not an object.
If you must work with raw arrays and want to use array_push, ensure you are pushing compatible data structures:
$competitions = Competition::all()->map(function ($c) {
return ['id' => $c->id, 'name' => $c->name];
});
$others = ['id' => 'others', 'name' => 'Others'];
array_push($competitions, $others);
// Now iterating safely
foreach ($competitions as $item) {
// Safe to access $item['name'] or $item['id']
}
Trade-offs and Best Practices
Using collect() allows you to chain methods like map, filter, and push fluently, which is generally preferred in Laravel for readability. It also prevents the collection from being destroyed if you modify it in place incorrectly.
Directly modifying an Eloquent collection returned by a query (e.g., $collection->push()) works, but be cautious about what you push. If your view relies on model attributes via magic methods (like $item->name), ensure pushed items are either models or arrays that mimic the necessary properties. The safest bet is to normalize the data early in the controller before any rendering happens.
This approach gives you full control over the dropdown content without violating database integrity. You can add multiple static options, reorder them by manipulating the collection, or conditionally include them based on logic, all while keeping your codebase clean and error-free.
