-2

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>
  • You're using Query Builder`DB::` instead of the Eloquent `Vehicle`, so there are no relations. Once you do use `Vehicle`, then it has many customers, so `customer->full_name` won't work because `$v->customer` is a collection of many records. – aynber Feb 02 '23 at 19:39
  • `->first()` **can return `null`**. You're calling `$v->customer`, which is `null->customer`. Just check it first, i.e. `if ($v)`, _then_ `$v->customer`. Or, use `->firstOrFail()` to trigger a 404 if you can't find a record in your database. And you're also not using your `Vehicle` model, so `$v->customer->full_name` also won't work. – Tim Lewis Feb 02 '23 at 19:39
  • Actually, you're using a join, so it would be `$v->full_name`. But as Tim pointed out, `$v` is null because it didn't find any records. – aynber Feb 02 '23 at 19:41
  • 1
    Also also, you have `(Vehicle $vehicle)`, which would load a `Vehicle` from the DB based on the ID from the URL (i.e. `/vehicles/1` would call `SELECT * FROM vehicles WHERE id = 1`). You're then **promptly overriding _ALL OF THAT_** when you call `$vehicle = DB::table(...)`... Check the documentation on how all of this works, and clean up your code. – Tim Lewis Feb 02 '23 at 19:43
  • Welcome to SO. Please try searching before posting a new question - searching for your error msg turns up many answers here already. As the error says, `$v` is a Collection, not a single Vehicle instance - think of it like an array of Vehicles. You need to iterate over it before you can access the model properties of each. – Don't Panic Feb 12 '23 at 11:42
  • Does this answer your question? [Property \[title\] does not exist on this collection instance](https://stackoverflow.com/questions/41366092/property-title-does-not-exist-on-this-collection-instance) – Don't Panic Feb 12 '23 at 11:42

1 Answers1

0

VEHICLE MODEL replace this

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

to

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

and VEHICLE CONTROLLER (show) replace to

$vehicle = Vehicle::with('customer')
                  ->paginate(10); 
helvete
  • 2,455
  • 13
  • 33
  • 37
Najmiddin
  • 31
  • 3