簡體   English   中英

Laravel 5.4 未定義變量錯誤 - controller 有問題?

[英]Laravel 5.4 Undefined variable error - problem with controller?

我收到此錯誤:未定義變量:sales_rep_names(視圖:C:\wamp64\www\VLCMRenewals\resources\views\search\index.blade.php)

當我嘗試在我的刀片中添加一個新的搜索下拉列表時。 這是我添加的導致問題的新代碼:

                {{--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>

最后,這是我在 Controller 中的代碼(制造商和客戶都有效):

    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 個不同的 arrays,用逗號分隔。 視圖參數是$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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM