Search code examples
phpeloquentlaravel-5.3

Laravel 5.3 Multiple Pluck in View undefined variable


i've been using Pluck to return data from other models to views fine. This model however, needs to return 4 bits of data for dropdown lists from 4 different models. Whenever I add more than 2 "Plucks" into the controller i get an undefined variable for one of them. 2 works fine though. Here's my code in my controller:

  public function create()
    {
        return view('products_alloweds.create', 
        ['products' => Products::pluck('product_name', 'id')],
        ['companies' => Companies::pluck('name', 'id')],
        ['deliveryaddress' => DeliveryAddresses::pluck('name', 'id')],
        ['customers' => Customers::pluck('name', 'id')]
        );
    }

Here's a sample my fields file that returns the data to the view:

<!-- Da Id Field -->
<div class="form-group col-sm-6">
    {!! Form::label('da_id', 'Da Id:') !!}
    {!! Form::number('da_id', $deliveryaddress, null, ['class' => 'form-control']) !!}

Like i said it works fine with just 2 but adding more than that results in an undefined variable error for either one of them.

Thanks


Solution

  • From the source code, second parameter of view() function should be an array as defined below:

    return view('products_alloweds.create', 
        [
            'products' => Products::pluck('product_name', 'id'),
            'companies' => Companies::pluck('name', 'id'),
            'deliveryaddress' => DeliveryAddresses::pluck('name', 'id'),
            'customers' => Customers::pluck('name', 'id')
        ]
    );
    

    whereas you are providing five parameters to the view() function.