简体   繁体   中英

Get specified record from belongsToMany relation Laravel

If I have properties table, and 2 other tables:

 *property_characteristics
  - property_id (i.e. 1)
  - characteristic_id (i.e. 5 - join with default_characteristics)
  - value (i.e. 3 - aka 3 rooms)
 *default_characteristics
  - id (i.e. 5)
  - name (i.e. rooms)

In the Property.php model I have:

public function characteristics()
{
return $this->belongsToMany('Proactiv\DefaultCharacteristic', 'property_characteristics', 'property_id', 'characteristic_id');
}

How can I get the number of rooms (value from property_characteristics) for a property starting from:

$property = Properties::find(1);

I would need something like this in view:

$property->characteristics->rooms // should return 3 which is the value columns on property_characteristics table

Since the value is on your pivot table, you need to tell Laravel about this extra field. Add to your belongsToMany line to make:

return $this->belongsToMany('Proactiv\DefaultCharacteristic', 'property_characteristics', 'property_id', 'characteristic_id')
    ->withPivot('value');

Then select characteristics with the name you want, rooms , and get the value:

echo $property->characteristics()->with('name', 'rooms')->first()->pivot->value;

Alternatively, add a getter to your Property model which does this for you (you'll still need to add that withPivot part to the relationship):

public function getRoomsAttribute()
{
    return $this->characteristics()
        ->where('name', 'rooms')
        ->first()
        ->pivot
        ->value;
}

Then you can get the number of rooms in a similar way to how you originally wanted to, with $property->rooms .

Or you could generalize this to get any characteristic:

public function getCharacteristic($name)
{
    return $this->characteristics()
        ->where('name', $name)
        ->first()
        ->pivot
        ->value;
}

And then get the number of rooms with $property->getCharacteristic('rooms') .

First, you have to tell your relationship to make your additional field available. You do this using the withPivot() method:

public function characteristics() {
    return $this->belongsToMany('Proactiv\DefaultCharacteristic', 'property_characteristics', 'property_id', 'characteristic_id')
        ->withPivot('value');
}

Now you can access your value on the pivot table. You do this like so:

$property = Properties::find(1);

foreach ($property->characteristics as $characteristic) {
    echo $characteristic->pivot->value;
}

You can read more about this in the documentation here , under the Retrieving Intermediate Table Columns heading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM