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