-1

Hi I am trying to get the model of a vehicle through hasOneThrough Relationship, but I am failing with the property, what am I doing wrong?

tables

vehicles
id
color
brand_id
brands
id
name
vehicle_models
id
name
brand_id

Models

Client

public function Vehicles(){

        return $this->belongstoMany(Vehicle::class);
    }

Vehicle

public function brand()
    {
        return $this->BelongsTo(Brand::class);
    }

    public function client(){

        return $this->belongstoMany(Client::class);
    }

   public function vehiclemodel(){

      return $this->hasOneThrough(VehicleModel::class, Brand::class,'brand_id','id','id','brand_id');
    }

Brand

 public function vehicles()
    {
        return $this->hasMany(Vehicle::class);
    }

VehicleModel

 public function brand()
    {
        return $this->hasMany(Brand::class);
    }

Query

$Client = Client::find($id);
        foreach($Client->vehicles->vehiclemodel as $c){
            echo $c->name;
        }
Raydenz8
  • 13
  • 3
  • This question has been asked before; please do a search for your error before posting. In your case, `$client->vehicles` is a `Collection`, so you can't access `->vehiclemodel` unless you loop. `foreach($client->vehicles as $vehicle){ echo $vehicle->vehiclemodel; }` (or similar) – Tim Lewis Apr 16 '21 at 14:31
  • For reference: https://stackoverflow.com/questions/41366092/property-title-does-not-exist-on-this-collection-instance, https://stackoverflow.com/questions/57782097/property-subcategory-does-not-exist-on-this-collection-instance, https://stackoverflow.com/questions/46846225/property-name-does-not-exist-on-this-collection-instance, https://stackoverflow.com/questions/43320223/property-id-does-not-exist-on-this-collection-instance; all of these are the same question, with answers avaialble. – Tim Lewis Apr 16 '21 at 14:35
  • 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) – miken32 Apr 16 '21 at 15:21

1 Answers1

0

Laravel Eloquent relations are as the name suggests 'eloquent'. This means that functions basically describe the structure of the code.

Having something, means that the thing that you have knows who is its owner, therefore the thing that has your id written on its forehead belongsTo you, while you haveOne(hasOne) thing or haveMany(hasMany) of them. The difference between those is that hasOne takes first, while hasMany takes all and puts them in the collection.

Belonging works the same way, the phone knows it belongs to a person because that person's name is written on top of the screen or whatever.

In your case a Brand hasOne(or hasMany) vehicles, as well as vehicle_models.

In this particular scenario that you described, both a vehicle and vehicle_model belong to a Brand, however because they both 'belong' they dont have a direct relationship with one another. Easiest way for you to get from one to another, would be by calling $vehicle->brand->vehicle_model or the other way round.

You should be also able to do (although not recommended, because its not for that kind of a relationship) that by using

$this->hasOneThrough(VehicleModel::class, Brand::class, 'id','id','brand_id','brand_id') 

if i recall correctly.

belongsToMany happens when you have a base of ids of several parents, that usually comes with an intermediate table which in here you dont have.

panjezor
  • 118
  • 8