-2

I am attempting to build a query that pulls from two tables with a one-to-many relationship. Rather than getting an array of data that exists for the relations, the output of the dd($customer); is:

0 => App\Models\Customer {#1496 ▼
      #connection: "mysql"
      #table: "customers"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:42 [▶]
      #original: array:42 [▶]
      #changes: []
      #casts: array:1 [▶]
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "xray_machine" => Illuminate\Database\Eloquent\Collection {#1538 …2}
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: array:34 [▶]
      #guarded: array:1 [▶]
      #forceDeleting: false
    }

Customer Model

public function xray_machine()
{
    return $this->hasMany(Xray_machine::class);
}

Xray_machine Model

public function customer()
{
    return $this->belongsTo(Customer::class);
}

Controller

public function epes_scheduled(){
    $customers = Customer::with('Xray_machine')->get();
    dd($customers);

    return view('work_orders/epe_scheduled', compact('customers'));
}

and in the view (by removing the dd() in controller I get this error:

Property [model] does not exist on this collection instance.

The View

@foreach ($customers as $customer)
    <tr>
        <td class="px-6 py-4 whitespace-nowrap">
            <div class="flex items-center">
                <div class="ml-4">
                    <div class="text-sm font-medium text-gray-900">
                        <a href="{{ route('edit_customer', ['id' => $customer->id]) }}">   @if($customer->customer_name) {{$customer->customer_name}} @endif</a>
                    </div>
                </div>
            </div>
        </td>
        <td class="px-6 py-4 whitespace-nowrap">
            <div class="flex items-center">
                <div class="ml-4">
                    <div class="text-sm font-medium text-gray-900">
                       @if($customer->Xray_machine->model) {{$customer->Xray_machine->model}} @endif
                    </div>
                </div>
            </div>
        </td>
    </tr>
@endforeach
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
T.A.
  • 638
  • 1
  • 6
  • 15
  • 1
    `$customers` is a `Collection` not an `array` because you are using `->get()`. Didn't you know that? – matiaslauriti Jan 13 '23 at 20:19
  • 3
    Laravel uses the [`Collection` Class](https://laravel.com/docs/9.x/collections) instead of arrays in a lot of places, but a `Collection` is essentially just an Object representation of an Array, with extended functionality. If you want an Array, use `->toArray()`. – Tim Lewis Jan 13 '23 at 20:33
  • 2
    For your edit, `$customer->Xray_machine->model` is unsafe code; if any `$customer` (since **you're looping multiple `Customer` records**) doesn't have an `Xray_machine`, your code is basically `$client->null->model`, which is not valid. `$customer->Xray_machine ? $customer->Xray_machine->model->model : 'N/A'` or `$customer->Xray_machine?->model`. Additionally, `Xray_machine` is a Collection of many `Xray_machine` models; your code doesn't know which one you're reference. `$client->Xray_machine?->first()?->model`, or `foreach($client->Xray_machine as $xrayMachine)`, then `$xrayMachine->model`. – Tim Lewis Jan 13 '23 at 20:39

1 Answers1

0

You are in right track and also the laravel is returning data perfectly. Laravel gives you the collection of the data instead of array. If you need array you can use toArray() method which gives you data in array.

 $customers = Customer::with('Xray_machine')->get()->toArray();
    dd($customers);
Subash Rijal
  • 163
  • 2
  • 9
  • Why would you do that? – matiaslauriti Jan 13 '23 at 20:50
  • @matiaslauriti I agree with your comment, but the "why" here is irrelevant The asker was asking why an Array was not returned, and this Answer, while a little generic, does accurately explain that it is a Collection, and if you want an array, use `->toArray()`. There aren't many cases where you'd want (or need) to do that, but this answer is "technically" correct. – Tim Lewis Jan 13 '23 at 20:54