Try access to show method in VehicleController() and return error: Attempt to read property 'customer' on null.
*ERROR: Property [full_name] does not exist on this collection instance
VEHICLE MODEL
class Vehicle extends Model
{
use HasFactory;
protected $table = 'vehicles';
protected $fillable = ['customer_id', 'model_name', 'brand',
'color', 'year', 'plate_number','engine_number', 'mileage', 'license_number',
'vehicle_type', 'photo_cover'
];
public function customer ()
{
return $this->hasMany(Customer::class);
}
}
CUSTOMER MODEL
class Customer extends Model
{
use HasFactory;
protected $table = 'customers';
protected $fillable = ['full_name', 'customer_type', 'email',
'phone_main', 'phone_mobile', 'doc_cpf', 'doc_cnpj', 'city', 'state',
'address', 'neighborhoods', 'number', 'zip_code'
];
public function vehicle ()
{
return $this->hasOne(Vehicle::class);
}
}
VEHICLE CONTROLLER (show)
public function show(Vehicle $vehicle)
{
$vehicle = Vehicle::select(
'vehicles.id', 'vehicles.model_name', 'vehicles.brand', 'vehicles.year', 'vehicles.color', 'vehicles.mileage',
'customers.full_name', 'vehicles.plate_number', 'vehicles.vehicle_type', 'vehicles.photo_cover'
)
->join('customers', 'customers.id', '=', 'vehicles.customer_id')
->get();
return view('admin.cars.show')->with('v', $vehicle);
}
VIEW (show.blade.php)
<?php
<td>CUSTOMER:</td>
<td class="view-row">
{{ $v->customer->full_name }}
</td>
<td>MODEL:</td>
<td class="view-row">
{{ $v->model_name }}
</td>
<td>BRAND:</td>
<td class="view-row">
{{ $v->brand }}
</td>
I don't know how to solve this, I made this change, but it didn't work. See below:
<td>CUSTOMER:</td>
<td class="view-row">
{{ $v->customer->full_name ?? 'Customer Empty' }}
</td>