Laravel 6控制器向视图传递多数组问题求助
Hey there, fellow Laravel newbie! Let’s break down how to pass those 8 dropdown datasets to your Blade view properly—this is such a common gotcha when you’re starting out, so don’t stress about it 😊
First, let’s cover the three reliable ways to pass multiple arrays/collections from your controller to Blade, and then we’ll troubleshoot the errors you’re seeing.
1. Chain multiple ->with() calls
The with() method is designed to be chained, so you can pass one variable at a time with clear key-value pairs:
public function loadDropdowns() { // Your Eloquent queries (make sure these return collections with get()/all()) $categories = Category::all(); $tags = Tag::where('is_active', true)->get(); $users = User::select('id', 'name')->get(); $statuses = Status::orderBy('order')->get(); // ... add the remaining 4 datasets here // Chain with() to pass each variable to the view return view('your-blade-template-name') ->with('categories', $categories) ->with('tags', $tags) ->with('users', $users) ->with('statuses', $statuses) // Keep adding the rest of your variables here ->with('priorities', $priorities); }
2. Pass an associative array directly to view()
You can also pass all your data in one go as the second argument to the view() helper—this is great for keeping things concise:
public function loadDropdowns() { $categories = Category::all(); $tags = Tag::where('is_active', true)->get(); // ... your other 6 datasets return view('your-blade-template-name', [ 'categories' => $categories, 'tags' => $tags, 'users' => $users, 'statuses' => $statuses, // List all your key-value pairs here ]); }
3. Use compact() (watch for variable name matches!)
compact() is handy if your controller variable names exactly match what you want to use in Blade. Just pass the variable names as strings to compact():
public function loadDropdowns() { // Make sure these variable names are exactly what you'll reference in Blade $categories = Category::all(); $tags = Tag::where('is_active', true)->get(); $users = User::select('id', 'name')->get(); // ... remaining datasets // compact() takes strings that match your variable names return view('your-blade-template-name', compact('categories', 'tags', 'users', 'statuses')); }
Now let’s fix those errors you’re seeing
The "undefined variable" or "trying to get property of non-object" messages usually boil down to a few simple mistakes:
- Mismatched variable names: Double-check that the key you passed (e.g.,
'categories') matches exactly what you’re using in Blade (e.g.,$categories—case-sensitive!). A typo like$categoryinstead of$categorieswill throw an undefined variable error. - Wrong Eloquent return type: If you used
first()instead ofget()/all(), you’ll get a single model object instead of a collection. Trying to loop over a single object with@foreachwill trigger the "non-object" error. Always useget()when you need a list of records for a dropdown. - Empty collections (not null!): If a query returns no results, Laravel gives you an empty collection, not
null. You can safely loop over it, but if you want to add a fallback, use@if($categories->isNotEmpty())before your@foreach. - Incorrect view path: Make sure the view name you’re passing to
view()matches the actual file path inresources/views/. For example,view('forms.dropdowns')points toresources/views/forms/dropdowns.blade.php—if the path is wrong, Laravel might load a different view that doesn’t have your variables.
Example Blade dropdown code
Once your data is passed correctly, your dropdowns will look like this:
<select name="category_id"> <option value="">Select a category</option> @foreach($categories as $category) <option value="{{ $category->id }}">{{ $category->name }}</option> @endforeach </select>
Start small if you’re still stuck—try passing just 2 variables first, confirm they show up in Blade, then add the rest one by one. That way you can catch any typos or issues early!
内容的提问来源于stack exchange,提问作者Pete Corbett

