Laravel 5.4 未定义的变量错误 - 控制器有问题?

Laravel 5.4 Undefined variable error - problem with controller?

我收到此错误:未定义变量:sales_rep_names(视图:C:\wamp64\www\VLCMRenewals\resources\views\search\index.blade.php)

当我尝试在 blade 中添加新的搜索下拉列表时。这是我添加的导致问题的新代码:

                {{--select Sales Representative --}}
                    <!-- adding sales rep placeholder
                    Can we make this a drop-down menu?

                    
                    need to make sure "name=" is correct -->
                    <div class="form-group col-md-4">
                    <label class="mr-sm-2" >Sales Representative</label>
                    <select class="custom-select mr-sm-2" name="primary_sales_rep">
                    <option selected></option>
                        @if(count($sales_rep_names) >0)
                            @foreach ($sales_rep_names as $sales_rep_name)
                                <option value='{{$sales_rep_name->Primary_Sales_Rep}}'>{{$sales_rep_name->Primary_Sales_Rep}}</option>
                            @endforeach
                        @endif
                        </select>
                    </div>

这是一个有效的下拉列表示例(我复制了格式):

{{--select Manufacturer --}}
<div class="form-group col-md-4" >
<label class="mr-sm-2" >Manufacturer Name</label>
<select class="custom-select mr-sm-2" name="company">
<option selected></option>
@if(count($manufacturer_names) >0)
@foreach ($manufacturer_names as $manufacturer_name)
<option value='{{$manufacturer_name->Manufacturer_Name}}'>{{$manufacturer_name->Manufacturer_Name}}</option>
@endforeach
@endif
</select>
</div>

最后,这是我在控制器中的代码(制造商和客户都工作):

    public function index()
    {
        // will need to add Sales Rep here
        $customer_names = DB::table('dbo.contract_view')
            ->distinct()
            ->orderBy('Customer_Name','asc')
            ->get(['Customer_Name']);
        $manufacturer_names = DB::table('dbo.contract_view')
            ->distinct()
            ->orderBy('Manufacturer_Name','asc')
            ->get(['Manufacturer_Name']);

        // when I tested with product part number it also gave me an error with index.blade.php. maybe this is the wrong spot?
        $sales_rep_names = DB::table('dbo.contract_view')
        ->distinct()
        ->orderBy('Primary_Sales_Rep','asc')
        ->get(['Primary_Sales_Rep']);
        return view('search.index', ['customer_names' => $customer_names], ['manufacturer_names' => $manufacturer_names], ['sales_rep_names' => $sales_rep_names]);
        
    }

有什么建议吗?

您将以逗号分隔的 3 个不同数组的形式发送变量。视图参数为 $view, $data, $mergeData,因此您发送的第四个参数将被忽略。

向视图传递变量的正确方法是这样的:

return view('search.index', [
    'customer_names'     => $customer_names, 
    'manufacturer_names' => $manufacturer_names, 
    'sales_rep_names'    => $sales_rep_names
]);
        

return view('search.index')
    ->with(compact('customer_names', 'manufacturer_names', 'sales_rep_names');

return view('search.index')
    ->with('customer_names', $customer_names)
    ->with('manufacturer_names', $manufacturer_names)
    ->with('sales_rep_names', $sales_rep_names)