You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 $category instead of $categories will throw an undefined variable error.
  • Wrong Eloquent return type: If you used first() instead of get()/all(), you’ll get a single model object instead of a collection. Trying to loop over a single object with @foreach will trigger the "non-object" error. Always use get() 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 in resources/views/. For example, view('forms.dropdowns') points to resources/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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:57:29