Home > Enterprise >  Laravel 5.4 Undefined variable error - problem with controller?
Laravel 5.4 Undefined variable error - problem with controller?

Time:03-20

I'm getting this error: Undefined variable: sales_rep_names (View: C:\wamp64\www\VLCMRenewals\resources\views\search\index.blade.php)

when I try adding a new search dropdown in my blade. This is the new code I've added that's causing the problem:

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

                    
                    need to make sure "name=" is correct -->
                    <div >
                    <label  >Sales Representative</label>
                    <select  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>

And here's an example of a dropdown that DOES work (I copied the format):

{{--select Manufacturer --}}
<div  >
<label  >Manufacturer Name</label>
<select  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>

Lastly, here's the code I have my in Controller (manufacturer and customer both work):

    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]);
        
    }

Any advice?

CodePudding user response:

You are sending the variables as 3 different arrays separated by commas. The view parameters are $view, $data, $mergeData, so the forth parameter you are sending is ignored.

The correct way to pass variables to the view is like this:

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

or

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

or

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