Search code examples
laravel-5selectdropdown

Dropdown menu from any table in database - Laravel


I have a form, which has some text field. Let me explain this field:

So_number from table sales_order
so_date from table sales_order
customer_name from table customers

and then I've got linked all of model and controller and views

My controller is linked of my 3 models. and I try to call with {{}} this, as same as like variable as I made,

I call the details with this

<td><a href="/so/{{$so->so_number}}/details">{{$so->so_number}}</a></td>

this is my web.php of details

Route::get('/so/{so_number}/details', 'So_Controller@details'); 

this is my So_Controller@details:

public function details($so_number)
{
    $data_so = \App\Sales_Order::all();
    $data_so_detail = \App\Sales_Order_Details::all();
    $data_customer = \App\Customer_Model::all();
    return view('salesorder.details', ['data_so_detail' => $data_so_detail, 'data_so' => $data_so, 'data_customer' => $data_customer]);
} 

This is my views of my details.index.php

<div class="modal-body">
  <div class="col-md-6">
    <div class="form-group form-animate-text" style="margin-top:40px !important;">
      <input type="text" name="so_number" value="{{$data_so->so_number}}" class="form-text" required>
      <span class="bar"></span>
      <label for="so_number">Sales Order Number</label>
    </div>
  </div>
  <div class="col-md-6">
    <div class="form-group form-animate-text" style="margin-top:40px !important;">
      <input type="text" name="so_date" value="{{$data_so->so_date}}" class="form-text" required>
      <span class="bar"></span>
      <label for="so_date">Sales Order Date.</label>
    </div>
  </div>
  <div class="col-md-6">
    <div class="form-group form-animate-text" style="margin-top:40px !important;">
      <label for="customer_name">Customer Name</label>
      <select name="customer_name" id="customer_name">
        @foreach ($data_customer as $customerName)
        <option value="{{$customerName->customer_name}}"></option>
        @endforeach
      </select>
    </div>
  </div>
  <div class="col-md-6">
    <div class="form-group form-animate-text" style="margin-top:40px !important;">
      <label for="customer_id"></label>
      <select name="customer_id" id="customer_id">
        @foreach ($data_customer as $customerId)
        <option value="{{$customerId->customer_id}}"></option>
        @endforeach
      </select>
    </div>
  </div>

And the error is

ErrorException (E_ERROR)
Property [so_number] does not exist on this collection instance. (View: /Applications/XAMPP/xamppfiles/htdocs/belajar_laravel/resources/views/salesorder/details.blade.php)
Previous exceptions

    Property [so_number] does not exist on this collection instance. (0)

My expected output is list of items in dropdown. Please help me.


Solution

  • In your details.blade.php, you have written

    {{$data_so->so_number}}

    whereas in controller, you have written

    $data_so = \App\Sales_Order::all();

    all() returns a collection which is like a 2 dimensional array. Either you have to iterate through $data_so to access 'so_number' or in controller you have to write

    $data_so = \App\Sales_Order::first(); //this is an example, first() or find() will return an instance of the model and you can directly access it like $data_so->so_number.

    In another case, check your method details($so_number). In case you want to retrieve from \App\Sales_Order a record for $so_number, you may use find().

    You may read https://laravel.com/docs/5.8/eloquent-relationships to connect multiple models.